diff --git a/selenium-junit-testng/pom.xml b/selenium-junit-testng/pom.xml new file mode 100644 index 0000000000..c6bd2b042c --- /dev/null +++ b/selenium-junit-testng/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + com.baeldung + selenium-junit-testng + 0.0.1-SNAPSHOT + + src + + + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + + + + + + + org.seleniumhq.selenium + selenium-java + 2.53.1 + + + junit + junit + 4.8.1 + + + org.testng + testng + 6.9.10 + + + \ 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 new file mode 100644 index 0000000000..6020b6bd2c --- /dev/null +++ b/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java @@ -0,0 +1,29 @@ +package main.java.com.baeldung.selenium; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; + +public class SeleniumExample { + + private WebDriver webDriver; + private final String url = "http://www.baeldung.com/"; + private final String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials"; + + public SeleniumExample() { + webDriver = new FirefoxDriver(); + webDriver.get(url); + } + + public void closeWindow() { + webDriver.close(); + } + + public String getActualTitle() { + return webDriver.getTitle(); + } + + public String getExpectedTitle() { + return expectedTitle; + } + +} diff --git a/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/TestSeleniumWithJUnit.java b/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/TestSeleniumWithJUnit.java new file mode 100644 index 0000000000..371f730eb9 --- /dev/null +++ b/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/TestSeleniumWithJUnit.java @@ -0,0 +1,30 @@ +package test.java.com.baeldung.selenium.junit; + +import static org.testng.Assert.assertEquals; +import main.java.com.baeldung.selenium.SeleniumExample; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestSeleniumWithJUnit { + + private SeleniumExample seleniumExample; + + @Before + public void setUp() { + seleniumExample = new SeleniumExample(); + } + + @After + public void tearDown() { + seleniumExample.closeWindow(); + } + + @Test + public void whenPageIsLoaded_thenTitleIsAsPerExpectation() { + String expectedTitle = seleniumExample.getExpectedTitle(); + String actualTitle = seleniumExample.getActualTitle(); + assertEquals(actualTitle, expectedTitle); + } +} diff --git a/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/TestSeleniumWithTestNG.java b/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/TestSeleniumWithTestNG.java new file mode 100644 index 0000000000..ec10f9ca82 --- /dev/null +++ b/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/TestSeleniumWithTestNG.java @@ -0,0 +1,30 @@ +package test.java.com.baeldung.selenium.testng; + +import static org.testng.Assert.assertEquals; +import main.java.com.baeldung.selenium.SeleniumExample; + +import org.testng.annotations.AfterSuite; +import org.testng.annotations.BeforeSuite; +import org.testng.annotations.Test; + +public class TestSeleniumWithTestNG { + + private SeleniumExample seleniumExample; + + @BeforeSuite + public void setUp() { + seleniumExample = new SeleniumExample(); + } + + @AfterSuite + public void tearDown() { + seleniumExample.closeWindow(); + } + + @Test + public void whenPageIsLoaded_thenTitleIsAsPerExpectation() { + String expectedTitle = seleniumExample.getExpectedTitle(); + String actualTitle = seleniumExample.getActualTitle(); + assertEquals(actualTitle, expectedTitle); + } +}