diff --git a/selenium-junit-testng/chromedriver.exe b/selenium-junit-testng/chromedriver.exe deleted file mode 100644 index f6ce07cb32..0000000000 Binary files a/selenium-junit-testng/chromedriver.exe and /dev/null differ diff --git a/selenium-junit-testng/chromedriver.linux b/selenium-junit-testng/chromedriver.linux deleted file mode 100755 index 1fb5667a0b..0000000000 Binary files a/selenium-junit-testng/chromedriver.linux and /dev/null differ diff --git a/selenium-junit-testng/chromedriver.mac b/selenium-junit-testng/chromedriver.mac deleted file mode 100755 index 9ccb03049b..0000000000 Binary files a/selenium-junit-testng/chromedriver.mac and /dev/null differ diff --git a/selenium-junit-testng/geckodriver.mac b/selenium-junit-testng/geckodriver.mac new file mode 100755 index 0000000000..84f66123a4 Binary files /dev/null and b/selenium-junit-testng/geckodriver.mac differ diff --git a/selenium-junit-testng/pom.xml b/selenium-junit-testng/pom.xml index d44a81639c..5b695ca900 100644 --- a/selenium-junit-testng/pom.xml +++ b/selenium-junit-testng/pom.xml @@ -66,6 +66,6 @@ 6.10 - 3.0.1 + 3.4.0 \ No newline at end of file diff --git a/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java b/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java index b97f66e9dd..c5ad5182ff 100644 --- a/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java +++ b/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java @@ -1,32 +1,31 @@ package main.java.com.baeldung.selenium; +import main.java.com.baeldung.selenium.config.SeleniumConfig; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.interactions.Actions; import java.util.List; import java.util.concurrent.TimeUnit; public class SeleniumExample { - private WebDriver webDriver; + private SeleniumConfig config; private String url = "http://www.baeldung.com/"; public SeleniumExample() { - System.setProperty("webdriver.firefox.marionette", "C:\\selenium\\geckodriver.exe"); - webDriver = new FirefoxDriver(); - webDriver.manage().window().maximize(); - webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); - webDriver.get(url); + config = new SeleniumConfig(); + config.getDriver().get(url); } public void closeWindow() { - webDriver.close(); + this.config.getDriver().close(); } public String getTitle() { - return webDriver.getTitle(); + return this.config.getDriver().getTitle(); } public void getAboutBaeldungPage() { @@ -36,7 +35,7 @@ public class SeleniumExample { } private void closeOverlay() { - List webElementList = webDriver.findElements(By.tagName("a")); + List webElementList = this.config.getDriver().findElements(By.tagName("a")); if (webElementList != null) { webElementList.stream() .filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title"))) @@ -47,14 +46,18 @@ public class SeleniumExample { } private void clickAboutLink() { - webDriver.findElement(By.partialLinkText("About")).click(); + this.config.getDriver().findElement(By.partialLinkText("About")).click(); } private void clickAboutUsLink() { - webDriver.findElement(By.partialLinkText("About Baeldung.")).click(); + Actions builder = new Actions(config.getDriver()); + WebElement element = this.config.getDriver().findElement(By.partialLinkText("About Baeldung.")); + builder.moveToElement(element).build().perform(); } public boolean isAuthorInformationAvailable() { - return webDriver.findElement(By.xpath("//*[contains(text(), 'an engineer with a passion for teaching and building stuff on the web')]")).isDisplayed(); + return this.config.getDriver() + .findElement(By.cssSelector("article > .row > div")) + .isDisplayed(); } } diff --git a/selenium-junit-testng/src/main/java/com/baeldung/selenium/config/SeleniumConfig.java b/selenium-junit-testng/src/main/java/com/baeldung/selenium/config/SeleniumConfig.java index 7a6eb2fb73..c84283b76b 100644 --- a/selenium-junit-testng/src/main/java/com/baeldung/selenium/config/SeleniumConfig.java +++ b/selenium-junit-testng/src/main/java/com/baeldung/selenium/config/SeleniumConfig.java @@ -4,9 +4,11 @@ import org.openqa.selenium.Capabilities; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.DesiredCapabilities; import java.io.File; +import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -15,30 +17,13 @@ public class SeleniumConfig { private WebDriver driver; public SeleniumConfig() { - Capabilities capabilities = DesiredCapabilities.chrome(); - driver = new ChromeDriver(capabilities); + Capabilities capabilities = DesiredCapabilities.firefox(); + driver = new FirefoxDriver(capabilities); + driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); } static { - String osName = getOsName("os.name").toLowerCase(); - final Matcher matcher = Pattern.compile("(mac|nux|win)").matcher(osName); - if (matcher.find()) { - switch (matcher.group()) { - case "mac": - System.setProperty("webdriver.chrome.driver", findFile("chromedriver.mac")); - break; - case "nux": - System.setProperty("webdriver.chrome.driver", findFile("chromedriver.linux")); - break; - case "win": - System.setProperty("webdriver.chrome.driver", findFile("chromedriver.exe")); - break; - } - } - } - - private static String getOsName(String prop) { - return (System.getProperty(prop)); + System.setProperty("webdriver.gecko.driver", findFile("geckodriver.mac")); } static private String findFile(String filename) { @@ -51,7 +36,7 @@ public class SeleniumConfig { } public void close() { - driver.quit(); + driver.close(); } public void navigateTo(String url) { diff --git a/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumWithJUnitLiveTest.java b/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumWithJUnitLiveTest.java index f05540542c..b1a4149358 100644 --- a/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumWithJUnitLiveTest.java +++ b/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumWithJUnitLiveTest.java @@ -10,7 +10,7 @@ import static org.testng.Assert.*; public class SeleniumWithJUnitLiveTest { private static SeleniumExample seleniumExample; - private String expectedTitle = "About Baeldung | Baeldung"; + private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials"; @BeforeClass public static void setUp() { diff --git a/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/SeleniumWithTestNGLiveTest.java b/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/SeleniumWithTestNGLiveTest.java index 2778e7eaa1..94426edc6d 100644 --- a/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/SeleniumWithTestNGLiveTest.java +++ b/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/SeleniumWithTestNGLiveTest.java @@ -10,7 +10,7 @@ import static org.testng.Assert.*; public class SeleniumWithTestNGLiveTest { private SeleniumExample seleniumExample; - private String expectedTitle = "About Baeldung | Baeldung"; + private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials"; @BeforeSuite public void setUp() {