├── .gitignore
├── README.md
├── cucumber-parallel
├── pom.xml
└── src
│ └── test
│ ├── java
│ └── com
│ │ └── automationrhapsody
│ │ └── cucumber
│ │ ├── formatter
│ │ ├── CustomFormatter.java
│ │ └── RunWikipediaTestsWithCustomFormatter.java
│ │ └── parallel
│ │ └── tests
│ │ └── wikipedia
│ │ ├── BaseSteps.java
│ │ ├── RunWikipediaTest.java
│ │ └── WikipediaSteps.java
│ └── resources
│ └── com
│ └── automationrhapsody
│ └── cucumber
│ └── parallel
│ └── tests
│ └── wikipedia
│ ├── ignored.feature
│ ├── main-page.feature
│ ├── search-ambiguous-many.feature
│ └── search-direct.feature
├── design-patterns
├── pom.xml
└── src
│ └── test
│ └── java
│ └── com
│ └── automationrhapsody
│ └── designpatterns
│ ├── Browser.java
│ ├── DesignPatternsTest.java
│ ├── HomePageObject.java
│ ├── NullWebElement.java
│ ├── WebDriverFacade.java
│ └── WebDriverFactory.java
├── pom.xml
└── webdrivers
├── IEDriver-64-win.exe
├── chromedriver-64-linux
├── chromedriver-64-win.exe
├── geckodriver-64-linux
└── geckodriver-64-win.exe
/.gitignore:
--------------------------------------------------------------------------------
1 | /.idea
2 | /*/target
3 | /*.iml
4 | /*/*.iml
5 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # selenium-samples-java #
2 |
3 | Java Selenium WebDriver samples that are used in my blog: Automation Rhapsody
4 |
5 | ## cucumber-parallel ##
6 |
7 | Code examples for following Cucumber posts:
8 | * Introduction to Cucumber and BDD with examples
9 | * Running Cucumber tests in parallel
10 | * Create Cucumber JVM custom formatter
11 |
12 | ## design-patterns ##
13 |
14 | Code examples for following posts:
15 | * Design patterns every test automation engineer should know series
16 | * Manage and automatically select needed WebDriver in Java 8 Selenium project
17 |
18 |
--------------------------------------------------------------------------------
/cucumber-parallel/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.automationrhapsody
8 | selenium-samples-java
9 | 1.0-SNAPSHOT
10 |
11 | com.automationrhapsody.cucumberparallel
12 | cucumber-parallel
13 | 1.0-SNAPSHOT
14 | cucumber-parallel
15 |
16 | 1.2.4
17 |
18 |
19 |
20 | info.cukes
21 | cucumber-java8
22 | ${cucumber.version}
23 | test
24 |
25 |
26 | info.cukes
27 | cucumber-junit
28 | ${cucumber.version}
29 | test
30 |
31 |
32 |
33 |
34 |
35 | com.github.temyers
36 | cucumber-jvm-parallel-plugin
37 | 2.2.0
38 |
39 |
40 | generateRunners
41 | validate
42 |
43 | generateRunners
44 |
45 |
46 | com.automationrhapsody.cucumber.parallel.tests.wikipedia
47 | src/test/resources/com
48 | target/cucumber-parallel
49 | json,html
50 | "~@ignored"
51 |
52 |
53 |
54 |
55 |
56 | org.apache.maven.plugins
57 | maven-surefire-plugin
58 | 2.19
59 |
60 | 10
61 | true
62 |
63 | **/Parallel*IT.class
64 |
65 |
66 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/cucumber-parallel/src/test/java/com/automationrhapsody/cucumber/formatter/CustomFormatter.java:
--------------------------------------------------------------------------------
1 | package com.automationrhapsody.cucumber.formatter;
2 |
3 | import java.util.List;
4 |
5 | import gherkin.formatter.Formatter;
6 | import gherkin.formatter.NiceAppendable;
7 | import gherkin.formatter.Reporter;
8 | import gherkin.formatter.model.Background;
9 | import gherkin.formatter.model.BasicStatement;
10 | import gherkin.formatter.model.Examples;
11 | import gherkin.formatter.model.Feature;
12 | import gherkin.formatter.model.Match;
13 | import gherkin.formatter.model.Result;
14 | import gherkin.formatter.model.Scenario;
15 | import gherkin.formatter.model.ScenarioOutline;
16 | import gherkin.formatter.model.Step;
17 | import gherkin.formatter.model.TagStatement;
18 |
19 | public class CustomFormatter implements Reporter, Formatter {
20 |
21 | private NiceAppendable output;
22 |
23 | public CustomFormatter(Appendable appendable) {
24 | output = new NiceAppendable(appendable);
25 | output.println("CustomFormatter()");
26 | System.out.println("CustomFormatter(): " + output.toString());
27 | }
28 |
29 | @Override
30 | public void syntaxError(String s, String s1, List list, String s2, Integer integer) {
31 | String out = String.format("syntaxError(): s=%s, s1=%s, list=%s, s2=%s, integer=%s", s, s1, list, s2, integer);
32 | System.out.println(out);
33 | }
34 |
35 | @Override
36 | public void uri(String s) {
37 | System.out.println("uri(): " + s);
38 | }
39 |
40 | @Override
41 | public void feature(Feature feature) {
42 | System.out.println("feature(): " + printTagStatement(feature));
43 | }
44 |
45 | @Override
46 | public void scenarioOutline(ScenarioOutline scenarioOutline) {
47 | System.out.println("scenarioOutline(): " + printTagStatement(scenarioOutline));
48 | }
49 |
50 | @Override
51 | public void examples(Examples examples) {
52 | String out = String.format("rows=%s", examples.getRows());
53 | System.out.println("examples(): " + printTagStatement(examples) + "; " + out);
54 | }
55 |
56 | @Override
57 | public void startOfScenarioLifeCycle(Scenario scenario) {
58 | System.out.println("startOfScenarioLifeCycle(): " + printTagStatement(scenario));
59 | }
60 |
61 | @Override
62 | public void background(Background background) {
63 | System.out.println("background(): " + printBasicStatement(background));
64 | }
65 |
66 | @Override
67 | public void scenario(Scenario scenario) {
68 | System.out.println("scenario(): " + printTagStatement(scenario));
69 | }
70 |
71 | @Override
72 | public void step(Step step) {
73 | String out = String.format("doc_string=%s, rows=%s", step.getDocString(), step.getRows());
74 | System.out.println("step(): " + printBasicStatement(step) + "; " + out);
75 | }
76 |
77 | @Override
78 | public void endOfScenarioLifeCycle(Scenario scenario) {
79 | System.out.println("endOfScenarioLifeCycle(): " + printTagStatement(scenario));
80 | }
81 |
82 | @Override
83 | public void done() {
84 | System.out.println("done()");
85 | }
86 |
87 | @Override
88 | public void close() {
89 | output.close();
90 | System.out.println("close()");
91 | }
92 |
93 | @Override
94 | public void eof() {
95 | System.out.println("eof()");
96 | }
97 |
98 | @Override
99 | public void before(Match match, Result result) {
100 | System.out.println("before(): " + printMatch(match) + "; " + printResult(result));
101 | }
102 |
103 | @Override
104 | public void result(Result result) {
105 | output.println(printResult(result));
106 | System.out.println("result(): " + printResult(result));
107 | }
108 |
109 | @Override
110 | public void after(Match match, Result result) {
111 | System.out.println("after(): " + printMatch(match) + "; " + printResult(result));
112 | }
113 |
114 | @Override
115 | public void match(Match match) {
116 | System.out.println("match(): " + printMatch(match));
117 | }
118 |
119 | @Override
120 | public void embedding(String s, byte[] bytes) {
121 | System.out.println("embedding(): s=");
122 | }
123 |
124 | @Override
125 | public void write(String s) {
126 | System.out.println("write(): " + s);
127 | }
128 |
129 | private String printBasicStatement(BasicStatement basic) {
130 | return String.format("keyword=%s, name=%s, line=%s", basic.getKeyword(), basic.getName(), basic.getLine());
131 | }
132 |
133 | private String printTagStatement(TagStatement tag) {
134 | return String.format("tags=%s, %s", tag.getTags(), printBasicStatement(tag));
135 | }
136 |
137 | private String printMatch(Match match) {
138 | return String.format("arguments=%s, location=%s", match.getArguments(), match.getLocation());
139 | }
140 |
141 | private String printResult(Result result) {
142 | return String.format("status=%s, duration=%s, error_message=%s",
143 | result.getStatus(), result.getDuration(), result.getErrorMessage());
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/cucumber-parallel/src/test/java/com/automationrhapsody/cucumber/formatter/RunWikipediaTestsWithCustomFormatter.java:
--------------------------------------------------------------------------------
1 | package com.automationrhapsody.cucumber.formatter;
2 |
3 | import org.junit.runner.RunWith;
4 |
5 | import cucumber.api.CucumberOptions;
6 | import cucumber.api.junit.Cucumber;
7 |
8 | @RunWith(Cucumber.class)
9 | @CucumberOptions(
10 | plugin = {"com.automationrhapsody.cucumber.formatter.CustomFormatter:custom-formatter-output.txt"},
11 | features = {"classpath:com/automationrhapsody/cucumber/parallel/tests/wikipedia"},
12 | glue = {"com.automationrhapsody.cucumber.parallel.tests"},
13 | tags = {"~@ignored"}
14 | )
15 | public class RunWikipediaTestsWithCustomFormatter {
16 | }
17 |
--------------------------------------------------------------------------------
/cucumber-parallel/src/test/java/com/automationrhapsody/cucumber/parallel/tests/wikipedia/BaseSteps.java:
--------------------------------------------------------------------------------
1 | package com.automationrhapsody.cucumber.parallel.tests.wikipedia;
2 |
3 | import java.io.File;
4 | import java.util.Properties;
5 |
6 | import org.openqa.selenium.WebDriver;
7 | import org.openqa.selenium.firefox.FirefoxDriver;
8 |
9 | public class BaseSteps {
10 |
11 | private static final String WEB_DRIVER_FOLDER = "webdrivers";
12 |
13 | protected WebDriver driver;
14 |
15 | protected void startWebDriver() {
16 | Properties props = System.getProperties();
17 | props.setProperty("webdriver.gecko.driver",
18 | driversFolder(new File("").getAbsolutePath()) + "geckodriver-64-win.exe");
19 | driver = new FirefoxDriver();
20 | driver.navigate().to("http://en.wikipedia.org");
21 | }
22 |
23 | protected void stopWebDriver() {
24 | driver.quit();
25 | }
26 |
27 | protected void wait(int timeOutInSeconds) {
28 | try {
29 | Thread.sleep(timeOutInSeconds * 1000);
30 | } catch (InterruptedException e) {
31 | e.printStackTrace();
32 | }
33 | }
34 |
35 | private static String driversFolder(String path) {
36 | File file = new File(path);
37 | for (String item : file.list()) {
38 | if (WEB_DRIVER_FOLDER.equals(item)) {
39 | return file.getAbsolutePath() + "/" + WEB_DRIVER_FOLDER + "/";
40 | }
41 | }
42 | return driversFolder(file.getParent());
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/cucumber-parallel/src/test/java/com/automationrhapsody/cucumber/parallel/tests/wikipedia/RunWikipediaTest.java:
--------------------------------------------------------------------------------
1 | package com.automationrhapsody.cucumber.parallel.tests.wikipedia;
2 |
3 | import org.junit.runner.RunWith;
4 |
5 | import cucumber.api.CucumberOptions;
6 | import cucumber.api.junit.Cucumber;
7 |
8 | @RunWith(Cucumber.class)
9 | @CucumberOptions(
10 | format = {
11 | "json:target/cucumber/wikipedia.json",
12 | "html:target/cucumber/wikipedia.html",
13 | "pretty"
14 | },
15 | tags = {"~@ignored"}
16 | )
17 | public class RunWikipediaTest {
18 | }
19 |
--------------------------------------------------------------------------------
/cucumber-parallel/src/test/java/com/automationrhapsody/cucumber/parallel/tests/wikipedia/WikipediaSteps.java:
--------------------------------------------------------------------------------
1 | package com.automationrhapsody.cucumber.parallel.tests.wikipedia;
2 |
3 | import java.time.LocalDate;
4 | import java.time.format.DateTimeFormatter;
5 |
6 | import org.openqa.selenium.By;
7 | import org.openqa.selenium.NoSuchElementException;
8 | import org.openqa.selenium.WebElement;
9 |
10 | import cucumber.api.java.After;
11 | import cucumber.api.java.Before;
12 | import cucumber.api.java.en.Given;
13 | import cucumber.api.java.en.Then;
14 | import cucumber.api.java.en.When;
15 |
16 | import static junit.framework.Assert.assertTrue;
17 | import static org.junit.Assert.assertEquals;
18 |
19 | public class WikipediaSteps extends BaseSteps {
20 |
21 | @Before
22 | public void before() {
23 | startWebDriver();
24 | }
25 |
26 | @After
27 | public void after() {
28 | stopWebDriver();
29 | }
30 |
31 | @Given("^Enter search term '(.*?)'$")
32 | public void searchFor(String searchTerm) {
33 | WebElement searchField = driver.findElement(By.id("searchInput"));
34 | searchField.sendKeys(searchTerm);
35 | }
36 |
37 | @When("^Do search$")
38 | public void clickSearchButton() {
39 | WebElement searchButton = driver.findElement(By.id("searchButton"));
40 | searchButton.click();
41 | wait(2);
42 | }
43 |
44 | @Then("^Multiple results are shown for '(.*?)'$")
45 | public void assertMultipleResults(String searchResults) {
46 | WebElement firstSearchResult = driver.findElement(By.cssSelector("div#mw-content-text.mw-content-ltr p"));
47 | assertEquals(searchResults, firstSearchResult.getText());
48 | }
49 |
50 | @Then("^Single result is shown for '(.*?)'$")
51 | public void assertSingleResult(String searchResults) {
52 | WebElement articleName = driver.findElement(By.id("firstHeading"));
53 | assertEquals(articleName.getText(), searchResults);
54 | }
55 |
56 | @Then("^This is (.*?)good article$")
57 | public void assertGoodArticle(String isNot) {
58 | boolean isGood = isNot != null && "".equals(isNot);
59 | try {
60 | driver.findElement(By.cssSelector("div#mw-indicator-good-star.mw-indicator a img"));
61 | assertTrue(isGood);
62 | } catch (NoSuchElementException enfe) {
63 | assertTrue(!isGood);
64 | }
65 | }
66 |
67 | @Then("^Current date is shown$")
68 | public void checkCurrentDate() {
69 | WebElement element = driver.findElement(By.cssSelector("div#mp-otd p b a"));
70 |
71 | String fullFormat = "%s";
72 | DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MMMM d");
73 | String expected = String.format(fullFormat, LocalDate.now().format(dateFormat));
74 |
75 | assertEquals(expected, element.getText());
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/cucumber-parallel/src/test/resources/com/automationrhapsody/cucumber/parallel/tests/wikipedia/ignored.feature:
--------------------------------------------------------------------------------
1 | @ignored
2 | Feature:
3 |
4 | Scenario:
5 | Then Not yet implemented feature
--------------------------------------------------------------------------------
/cucumber-parallel/src/test/resources/com/automationrhapsody/cucumber/parallel/tests/wikipedia/main-page.feature:
--------------------------------------------------------------------------------
1 | Feature:
2 |
3 | Scenario:
4 | Then Current date is shown
--------------------------------------------------------------------------------
/cucumber-parallel/src/test/resources/com/automationrhapsody/cucumber/parallel/tests/wikipedia/search-ambiguous-many.feature:
--------------------------------------------------------------------------------
1 | Feature:
2 |
3 | Scenario Outline:
4 | Given Enter search term ''
5 | When Do search
6 | Then Multiple results are shown for ''
7 |
8 | Examples:
9 | | searchTerm | result |
10 | | mercury | Mercury usually refers to: |
11 | | max | Max or MAX may refer to: |
--------------------------------------------------------------------------------
/cucumber-parallel/src/test/resources/com/automationrhapsody/cucumber/parallel/tests/wikipedia/search-direct.feature:
--------------------------------------------------------------------------------
1 | Feature:
2 |
3 | Scenario: direct search for good article
4 | Given Enter search term 'Freddie Mercury'
5 | When Do search
6 | Then Single result is shown for 'Freddie Mercury'
7 | And This is good article
8 |
9 | Scenario: direct search for not good article
10 | Given Enter search term 'Cucumber'
11 | When Do search
12 | Then Single result is shown for 'Cucumber'
13 | And This is not good article
--------------------------------------------------------------------------------
/design-patterns/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.automationrhapsody
8 | selenium-samples-java
9 | 1.0-SNAPSHOT
10 |
11 | com.automationrhapsody.designpatterns
12 | design-patterns
13 | 1.0-SNAPSHOT
14 | design-patterns
15 |
16 |
17 | org.testng
18 | testng
19 | 6.8.21
20 | test
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/design-patterns/src/test/java/com/automationrhapsody/designpatterns/Browser.java:
--------------------------------------------------------------------------------
1 | package com.automationrhapsody.designpatterns;
2 |
3 | import java.util.Arrays;
4 | import java.util.function.Supplier;
5 |
6 | import org.openqa.selenium.WebDriver;
7 | import org.openqa.selenium.chrome.ChromeDriver;
8 | import org.openqa.selenium.firefox.FirefoxDriver;
9 | import org.openqa.selenium.ie.InternetExplorerDriver;
10 |
11 | public enum Browser {
12 | FIREFOX("gecko", FirefoxDriver::new),
13 | CHROME("chrome", ChromeDriver::new),
14 | IE("ie", InternetExplorerDriver::new);
15 |
16 | private String name;
17 | private Supplier driverSupplier;
18 |
19 | Browser(String name, Supplier driverSupplier) {
20 | this.name = name;
21 | this.driverSupplier = driverSupplier;
22 | }
23 |
24 | public String getName() {
25 | return name;
26 | }
27 |
28 | public WebDriver getDriver() {
29 | return driverSupplier.get();
30 | }
31 |
32 | public static Browser fromString(String value) {
33 | for (Browser browser : values()) {
34 | if (value != null && value.toLowerCase().equals(browser.getName())) {
35 | return browser;
36 | }
37 | }
38 | System.out.println("Invalid driver name passed as 'browser' system property. "
39 | + "One of: " + Arrays.toString(values()) + " is expected. Defaulting to Firefox.");
40 | return FIREFOX;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/design-patterns/src/test/java/com/automationrhapsody/designpatterns/DesignPatternsTest.java:
--------------------------------------------------------------------------------
1 | package com.automationrhapsody.designpatterns;
2 |
3 | import org.openqa.selenium.By;
4 | import org.openqa.selenium.WebElement;
5 | import org.testng.Assert;
6 | import org.testng.annotations.AfterClass;
7 | import org.testng.annotations.BeforeClass;
8 | import org.testng.annotations.Test;
9 |
10 | public class DesignPatternsTest {
11 |
12 | private static final String URL = "http://automationrhapsody.com/examples/utf8icons.html";
13 |
14 | private WebDriverFacade webDriver;
15 |
16 | @BeforeClass
17 | public void setUp() {
18 | webDriver = new WebDriverFacade();
19 | webDriver.start(URL);
20 | }
21 |
22 | @Test
23 | public void nullObjectPattern() {
24 | // Location of elements is inside tests logic which is not good practice
25 | WebElement element = webDriver.findElement(By.cssSelector("notExisting"));
26 | element.click();
27 |
28 | // Note in .equals() we first put object that we are sure is not null
29 | // This is proper way to do it, otherwise NPE will be thrown
30 | if (NullWebElement.getNull().equals(element)) {
31 | System.out.println("NullWebElement.getNull().equals(element)");
32 | }
33 | Assert.assertTrue(NullWebElement.getNull().equals(element));
34 |
35 | // Special method is created to check for null
36 | if (NullWebElement.isNull(element)) {
37 | System.out.println("NullWebElement.isNull(element)");
38 | }
39 | Assert.assertTrue(NullWebElement.isNull(element));
40 |
41 | // Because we use singleton it is possible to compare even references
42 | if (element == NullWebElement.getNull()) {
43 | System.out.println("element == NullWebElement.getNull()");
44 | }
45 | Assert.assertTrue(element == NullWebElement.getNull());
46 | }
47 |
48 | @Test
49 | public void pageObjectPattern() {
50 | HomePageObject homePage = new HomePageObject(webDriver);
51 | // One element is defined on only one place - we do not repeat ourselves
52 | System.out.println("Search label is: " + homePage.getSearchLabel());
53 | Assert.assertEquals(homePage.getSearchLabel(), "Search:");
54 | homePage.clearSearch();
55 | homePage.searchFor("automation");
56 | }
57 |
58 | @AfterClass
59 | public void tearDown() {
60 | webDriver.stop();
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/design-patterns/src/test/java/com/automationrhapsody/designpatterns/HomePageObject.java:
--------------------------------------------------------------------------------
1 | package com.automationrhapsody.designpatterns;
2 |
3 | import org.openqa.selenium.By;
4 | import org.openqa.selenium.WebElement;
5 |
6 | public class HomePageObject {
7 |
8 | private WebDriverFacade webDriver;
9 |
10 | public HomePageObject(WebDriverFacade webDriver) {
11 | this.webDriver = webDriver;
12 | }
13 |
14 | private WebElement getSearchField() {
15 | return webDriver.findElement(By.id("search"));
16 | }
17 |
18 | public String getSearchLabel() {
19 | WebElement element = webDriver.findElement(By.cssSelector("div.find label"));
20 | return element.getText();
21 | }
22 |
23 | public void searchFor(String text) {
24 | getSearchField().sendKeys(text);
25 | }
26 |
27 | public void clearSearch() {
28 | webDriver.executeJavaScript("$('span.cancel').click()");
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/design-patterns/src/test/java/com/automationrhapsody/designpatterns/NullWebElement.java:
--------------------------------------------------------------------------------
1 | package com.automationrhapsody.designpatterns;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.openqa.selenium.By;
7 | import org.openqa.selenium.Dimension;
8 | import org.openqa.selenium.OutputType;
9 | import org.openqa.selenium.Point;
10 | import org.openqa.selenium.Rectangle;
11 | import org.openqa.selenium.WebDriverException;
12 | import org.openqa.selenium.WebElement;
13 |
14 | public class NullWebElement implements WebElement {
15 |
16 | private NullWebElement() {
17 | }
18 |
19 | private static NullWebElement instance;
20 |
21 | public static NullWebElement getNull() {
22 | if (instance == null) {
23 | instance = new NullWebElement();
24 | }
25 | return instance;
26 | }
27 |
28 | public static boolean isNull(WebElement element) {
29 | return getNull().equals(element);
30 | }
31 |
32 | @Override
33 | public void click() {
34 | }
35 |
36 | @Override
37 | public void submit() {
38 | }
39 |
40 | @Override
41 | public void sendKeys(CharSequence... charSequences) {
42 | }
43 |
44 | @Override
45 | public void clear() {
46 | }
47 |
48 | @Override
49 | public String getTagName() {
50 | return "";
51 | }
52 |
53 | @Override
54 | public String getAttribute(String s) {
55 | return "";
56 | }
57 |
58 | @Override
59 | public boolean isSelected() {
60 | return false;
61 | }
62 |
63 | @Override
64 | public boolean isEnabled() {
65 | return false;
66 | }
67 |
68 | @Override
69 | public String getText() {
70 | return "";
71 | }
72 |
73 | @Override
74 | public List findElements(By by) {
75 | return new ArrayList<>();
76 | }
77 |
78 | @Override
79 | public WebElement findElement(By by) {
80 | return NullWebElement.getNull();
81 | }
82 |
83 | @Override
84 | public boolean isDisplayed() {
85 | return false;
86 | }
87 |
88 | @Override
89 | public Point getLocation() {
90 | return new Point(0, 0);
91 | }
92 |
93 | @Override
94 | public Dimension getSize() {
95 | return new Dimension(0, 0);
96 | }
97 |
98 | @Override
99 | public Rectangle getRect() {
100 | return new Rectangle(0, 0, 0, 0);
101 | }
102 |
103 | @Override
104 | public String getCssValue(String s) {
105 | return "";
106 | }
107 |
108 | @Override
109 | public X getScreenshotAs(OutputType outputType) throws WebDriverException {
110 | return null;
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/design-patterns/src/test/java/com/automationrhapsody/designpatterns/WebDriverFacade.java:
--------------------------------------------------------------------------------
1 | package com.automationrhapsody.designpatterns;
2 |
3 | import java.util.List;
4 |
5 | import org.openqa.selenium.By;
6 | import org.openqa.selenium.JavascriptExecutor;
7 | import org.openqa.selenium.WebDriver;
8 | import org.openqa.selenium.WebElement;
9 | import org.openqa.selenium.support.ui.ExpectedConditions;
10 | import org.openqa.selenium.support.ui.WebDriverWait;
11 |
12 | public class WebDriverFacade {
13 |
14 | private static final long WAIT_SECONDS = 5;
15 |
16 | private WebDriver driver;
17 |
18 | public WebDriverFacade() {
19 | driver = WebDriverFactory.createWebDriver();
20 | }
21 |
22 | public void start(String url) {
23 | driver.get(url);
24 | }
25 |
26 | public void stop() {
27 | driver.quit();
28 | }
29 |
30 | public Object executeJavaScript(String script) {
31 | return ((JavascriptExecutor) driver).executeScript(script);
32 | }
33 |
34 | public WebElement findElement(By by) {
35 | try {
36 | WebDriverWait wait = new WebDriverWait(driver, WAIT_SECONDS);
37 | return wait.until(ExpectedConditions.visibilityOfElementLocated(by));
38 | } catch (Exception ex) {
39 | return NullWebElement.getNull();
40 | }
41 | }
42 |
43 | public List findElements(By by) {
44 | WebDriverWait wait = new WebDriverWait(driver, WAIT_SECONDS);
45 | return wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));
46 | }
47 | }
--------------------------------------------------------------------------------
/design-patterns/src/test/java/com/automationrhapsody/designpatterns/WebDriverFactory.java:
--------------------------------------------------------------------------------
1 | package com.automationrhapsody.designpatterns;
2 |
3 | import java.io.File;
4 |
5 | import org.openqa.selenium.WebDriver;
6 |
7 | class WebDriverFactory {
8 |
9 | private static final String WEB_DRIVER_FOLDER = "webdrivers";
10 |
11 | public static WebDriver createWebDriver() {
12 | Browser browser = Browser.fromString(System.getProperty("browser"));
13 | String arch = System.getProperty("os.arch").contains("64") ? "64" : "32";
14 | String os = System.getProperty("os.name").toLowerCase().contains("win") ? "win.exe" : "linux";
15 | String driverFileName = browser.getName() + "driver-" + arch + "-" + os;
16 | String driverFilePath = driversFolder(new File("").getAbsolutePath());
17 | System.setProperty("webdriver." + browser.getName() + ".driver", driverFilePath + driverFileName);
18 | return browser.getDriver();
19 | }
20 |
21 | private static String driversFolder(String path) {
22 | File file = new File(path);
23 | for (String item : file.list()) {
24 | if (WEB_DRIVER_FOLDER.equals(item)) {
25 | return file.getAbsolutePath() + "/" + WEB_DRIVER_FOLDER + "/";
26 | }
27 | }
28 | return driversFolder(file.getParent());
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 | com.automationrhapsody
5 | selenium-samples-java
6 | 1.0-SNAPSHOT
7 | pom
8 | selenium-samples-java
9 |
10 | UTF-8
11 | 1.8
12 | 1.8
13 |
14 |
15 | design-patterns
16 | cucumber-parallel
17 |
18 |
19 |
20 | org.seleniumhq.selenium
21 | selenium-server
22 | 3.4.0
23 |
24 |
25 |
--------------------------------------------------------------------------------
/webdrivers/IEDriver-64-win.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/llatinov/selenium-samples-java/a6d8aedd081e8dda1b7ab6569f071d52ba385b3b/webdrivers/IEDriver-64-win.exe
--------------------------------------------------------------------------------
/webdrivers/chromedriver-64-linux:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/llatinov/selenium-samples-java/a6d8aedd081e8dda1b7ab6569f071d52ba385b3b/webdrivers/chromedriver-64-linux
--------------------------------------------------------------------------------
/webdrivers/chromedriver-64-win.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/llatinov/selenium-samples-java/a6d8aedd081e8dda1b7ab6569f071d52ba385b3b/webdrivers/chromedriver-64-win.exe
--------------------------------------------------------------------------------
/webdrivers/geckodriver-64-linux:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/llatinov/selenium-samples-java/a6d8aedd081e8dda1b7ab6569f071d52ba385b3b/webdrivers/geckodriver-64-linux
--------------------------------------------------------------------------------
/webdrivers/geckodriver-64-win.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/llatinov/selenium-samples-java/a6d8aedd081e8dda1b7ab6569f071d52ba385b3b/webdrivers/geckodriver-64-win.exe
--------------------------------------------------------------------------------