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 7fde09d817..7a6eb2fb73 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 @@ -7,6 +7,8 @@ import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.remote.DesiredCapabilities; import java.io.File; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class SeleniumConfig { @@ -18,14 +20,20 @@ public class SeleniumConfig { } static { - if (getOsName("os.name").toLowerCase().contains("mac")) { - System.setProperty("webdriver.chrome.driver", findFile("chromedriver.mac")); - } else if (getOsName("os.name").toLowerCase().contains("nix") || - getOsName("os.name").toLowerCase().contains("nux") || - getOsName("os.name").toLowerCase().contains("aix")) { - System.setProperty("webdriver.chrome.driver", findFile("chromedriver.linux")); - } else if (getOsName("os.name").toLowerCase().contains("win")) { - System.setProperty("webdriver.chrome.driver", findFile("chromedriver.exe")); + 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; + } } } diff --git a/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumPageObjectTest.java b/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumPageObjectTest.java new file mode 100644 index 0000000000..f47110f86e --- /dev/null +++ b/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumPageObjectTest.java @@ -0,0 +1,50 @@ +package test.java.com.baeldung.selenium.junit; + +import main.java.com.baeldung.selenium.config.SeleniumConfig; +import main.java.com.baeldung.selenium.models.BaeldungAbout; +import main.java.com.baeldung.selenium.pages.BaeldungHomePage; +import main.java.com.baeldung.selenium.pages.StartHerePage; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +public class SeleniumPageObjectTest { + + private SeleniumConfig config; + private BaeldungHomePage homePage; + private BaeldungAbout about; + + @Before + public void setUp() { + config = new SeleniumConfig(); + homePage = new BaeldungHomePage(config); + about = new BaeldungAbout(config); + } + + @After + public void teardown() { + config.close(); + } + + @Test + public void givenHomePage_whenNavigate_thenTitleMatch() { + homePage.navigate(); + assertThat(homePage.getPageTitle(), is("Baeldung")); + } + + @Test + public void givenHomePage_whenNavigate_thenShouldBeInStartHere() { + homePage.navigate(); + StartHerePage startHerePage = homePage.clickOnStartHere(); + assertThat(startHerePage.getPageTitle(), is("Start Here")); + } + + @Test + public void givenAboutPage_whenNavigate_thenTitleMatch() { + about.navigateTo(); + assertThat(about.getPageTitle(), is("About Baeldung")); + } +}