Commits

Micha Kops  committed 0ec97ee

PageObjects example added

  • Participants
  • Parent commits 570534e

Comments (0)

Files changed (4)

File src/test/java/com/hascode/tutorial/DzoneHomePage.java

+package com.hascode.tutorial;
+
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.support.FindBy;
+import org.openqa.selenium.support.How;
+
+public class DzoneHomePage {
+	@FindBy(how = How.ID, using = "mh_searchQuery")
+	private WebElement searchBox;
+
+	public void searchFor(final String term) {
+		searchBox.sendKeys(term);
+		searchBox.submit();
+	}
+
+}

File src/test/java/com/hascode/tutorial/DzoneSearchResultsPage.java

+package com.hascode.tutorial;
+
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.support.FindBy;
+import org.openqa.selenium.support.How;
+
+public class DzoneSearchResultsPage {
+	@FindBy(how = How.ID, using = "content-inner")
+	private WebElement searchResultWrapper;
+
+	boolean containsSearchResult(final String term) {
+		// for tutorial purpose only ;)
+		return searchResultWrapper.getText().contains(term);
+	}
+}

File src/test/java/com/hascode/tutorial/UsingPageObjectsTest.java

+package com.hascode.tutorial;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.firefox.FirefoxDriver;
+import org.openqa.selenium.support.PageFactory;
+
+public class UsingPageObjectsTest {
+	private WebDriver driver;
+
+	@Before
+	public void setUp() {
+		driver = new FirefoxDriver();
+	}
+
+	@After
+	public void tearDown() {
+		driver.close();
+	}
+
+	@Test
+	public void testSearchDzone() {
+		driver.get("http://www.dzone.com/links/index.html");
+		DzoneHomePage homePage = PageFactory.initElements(driver,
+				DzoneHomePage.class);
+		homePage.searchFor("message driven beans");
+		DzoneSearchResultsPage resultsPage = PageFactory.initElements(driver,
+				DzoneSearchResultsPage.class);
+		assertTrue(resultsPage
+				.containsSearchResult("Message Driven Beans in Java EE 6"));
+	}
+}

File src/test/java/com/hascode/tutorial/WebpageTest.java

 
 	@Test
 	public void testGetTitle() {
-		driver.get("http://www.hascode.com/");
-		assertEquals("hasCode.com", driver.getTitle());
-		WebElement searchBox = driver.findElement(By.id("s"));
-		searchBox.sendKeys("ejb");
+		driver.get("http://www.dzone.com/links/index.html");
+		assertEquals("dzone.com - fresh links for developers",
+				driver.getTitle());
+		WebElement searchBox = driver.findElement(By.id("mh_searchQuery"));
+		searchBox.sendKeys("message driven beans");
 		searchBox.submit();
-		assertEquals("hasCode.com » Search Results » ejb", driver.getTitle());
+		assertEquals("Search Results For: message driven beans",
+				driver.getTitle());
 	}
 }