How to use Interface Decorated class of org.openqa.selenium.support.decorators package

Best Selenium code snippet using org.openqa.selenium.support.decorators.Interface Decorated

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18package org.openqa.selenium.support.decorators;
19
20import static org.assertj.core.api.Assertions.assertThat;
21import static org.mockito.Mockito.mock;
22
23import org.junit.Test;
24import org.junit.experimental.categories.Category;
25import org.openqa.selenium.WebDriver;
26import org.openqa.selenium.testing.UnitTests;
27
28@Category(UnitTests.class)
29public class InterfacesTest {
30
31  private interface SomeOtherInterface {}
32
33  private interface ExtendedDriver extends WebDriver, SomeOtherInterface {}
34
35  @Test
36  public void shouldNotAddInterfacesNotAvailableInTheOriginalDriver() {
37    WebDriver driver = mock(WebDriver.class);
38    assertThat(driver).isNotInstanceOf(SomeOtherInterface.class);
39
40    WebDriver decorated = new WebDriverDecorator().decorate(driver);
41    assertThat(decorated).isNotInstanceOf(SomeOtherInterface.class);
42  }
43
44  @Test
45  public void shouldRespectInterfacesAvailableInTheOriginalDriver() {
46    WebDriver driver = mock(ExtendedDriver.class);
47    assertThat(driver).isInstanceOf(SomeOtherInterface.class);
48
49    WebDriver decorated = new WebDriverDecorator().decorate(driver);
50    assertThat(decorated).isInstanceOf(SomeOtherInterface.class);
51  }
52}
53
Full Screen
copy
1Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(source);
2String title = XPathFactory.newInstance().newXPath().evaluate("//title", doc);
3
Full Screen
copy
1class MyRepository
2   void add(entity) {...}
3   void findByName(name) {...}
4
Full Screen
copy
1 class MyService() {
2
3   @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.SERIALIZABLE)
4   void doIt() {
5      var entity = myRepository.findByName("some-name");
6      if(record.field.equal("expected")) {
7        ... 
8        myRepository.add(newEntity)
9      }
10   }
11
12 }
13
Full Screen
copy
1 class TaskRepository
2   @Transactional(propagation=Propagation.REQUIRED, 
3   isolation=Isolation.SERIALIZABLE)
4   void add(entity) {
5      var name = entity.getName()
6      var found = this.findFirstByName(name);
7      if(found == null || found.getStatus().equal("in-progress")) 
8      {
9        .. do insert
10      }
11   }
12   @Transactional
13   void findFirstByName(name) {...}
14
Full Screen
copy
1  class Service {
2    @Transactional(isolation=.., propagation=...) // where .. are different from what is defined in taskRepository()
3    void doStuff() {
4      taskRepository.add(task);
5    }
6  }
7
Full Screen
copy
1    Icecream ic = new RainbowTopUp(new ChocoTopUp(new Vanilla()));
2
Full Screen
copy
1new BufferedReader( new FileInputStream() ).readLine();
2
Full Screen
copy
11- We can't change the existing legacy code
22- We want to extend the functionality
3
Full Screen
copy
1public interface Greeter {
2    String getMessageOfTheDay();
3}
4
Full Screen
copy
1public class BasicGreeter implements Greeter {
2
3    @Override
4    public String getMessageOfTheDay() {
5        return "Welcome to my server";
6    }
7
8}
9
Full Screen
copy
1public abstract class GreeterDecorator implements Greeter {
2
3    protected Greeter greeter;
4
5    public GreeterDecorator(Greeter greeter) {
6        this.greeter = greeter;
7    }
8
9    public String getMessageOfTheDay() {
10        return greeter.getMessageOfTheDay();
11    }
12
13}
14
Full Screen
copy
1public class StrangerDecorator extends GreeterDecorator {
2
3    public StrangerDecorator(Greeter greeter) {
4        super(greeter);
5    }
6
7    @Override
8    public String getMessageOfTheDay() {
9        return "Hello Stranger " + super.getMessageOfTheDay();
10    }
11
12}
13
Full Screen
copy
1public class DecoratorDemo {
2
3    public static void main(String[] args) {
4        Greeter greeter = new BasicGreeter();
5
6        String motd = greeter.getMessageOfTheDay();
7
8        System.out.println(motd);
9
10        Greeter newGreeter = new StrangerDecorator(greeter);
11
12        String newMotd = newGreeter.getMessageOfTheDay();
13
14        System.out.println(newMotd);
15
16        Greeter muchNewGreeter = new StrangerDecorator(new StrangerDecorator(greeter));
17
18        String newestMotd = muchNewGreeter.getMessageOfTheDay();
19
20        System.out.println(newestMotd);
21    }
22
23}
24
Full Screen
copy
1import java.io.File;
2import java.io.FileOutputStream;
3import java.io.IOException;
4import java.io.OutputStream;
5
6public class FileWriterDemo {
7
8    public static void main(String[] args) throws IOException {
9        File file = new File("./normal.txt");
10        file.createNewFile();
11
12        OutputStream oStream = new FileOutputStream(file);
13
14        String content = "I love Commodore 64";
15
16        oStream.write(content.getBytes());
17
18        oStream.close();
19    }
20
21}
22
Full Screen
copy
1public class JSONStream extends OutputStream {
2
3    protected OutputStream outputStream;
4
5    public JSONStream(OutputStream outputStream) {
6        this.outputStream = outputStream;
7    }
8
9    @Override
10    public void write(int b) throws IOException {
11        outputStream.write(b);
12    }
13
14    @Override
15    public void write(byte[] b) throws IOException {
16        String content = new String(b);
17
18        content = "{\r\n\tdata:\"" + content + "\"\r\n}";
19
20        outputStream.write(content.getBytes());
21    }
22
23}
24
Full Screen
copy
1public class JSONDecoratorDemo {
2
3    public static void main(String[] args) throws IOException {
4        File file = new File("./json.txt");
5        file.createNewFile();
6
7        OutputStream oStream = new FileOutputStream(file);
8
9        JSONStream js = new JSONStream(oStream);
10
11        String content = "I love Commodore 64";
12
13        js.write(content.getBytes());
14
15        js.close();
16        oStream.close();
17    }
18
19}
20
Full Screen
copy
1{
2    data:"I love Commodore 64"
3}
4
Full Screen
copy
1AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, 
2InputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, 
3StringBufferInputStream
4
Full Screen
copy
1public class FilterInputStream
2extends InputStream
3
Full Screen
copy
1BufferedInputStream, CheckedInputStream, CipherInputStream, DataInputStream, 
2DeflaterInputStream, DigestInputStream, InflaterInputStream, 
3LineNumberInputStream, ProgressMonitorInputStream, PushbackInputStream
4
Full Screen
copy
1BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("a.txt")));
2while(bis.available()>0)
3{
4        char c = (char)bis.read();
5        System.out.println("Char: "+c);;
6}
7
Full Screen
copy
1FileInputStream fis = new FileInputStream("/objects.gz");
2
Full Screen
copy
1BufferedInputStream bis = new BufferedInputStream(fis);
2
Full Screen
copy
1GzipInputStream gis = new GzipInputStream(bis);
2
Full Screen
copy
1ObjectInputStream ois = new ObjectInputStream(gis);
2
Full Screen
copy
1SomeObject someObject = (SomeObject) ois.readObject();
2// ...
3
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in Interface-Decorated

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)