├── .gitignore
├── .project
├── src
└── test
│ └── java
│ └── uk
│ └── co
│ └── activelylazy
│ └── selenium
│ ├── AmazonSearchResultsPage.java
│ ├── BetterAmazonSearchTest.java
│ ├── AmazonHomePage.java
│ └── SimpleAmazonSearchTest.java
├── pom.xml
└── .classpath
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | .settings
3 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 | seleniumguide
3 | NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.
4 |
5 |
6 |
7 | org.eclipse.jdt.core.javabuilder
8 |
9 |
10 |
11 | org.eclipse.jdt.core.javanature
12 |
13 |
--------------------------------------------------------------------------------
/src/test/java/uk/co/activelylazy/selenium/AmazonSearchResultsPage.java:
--------------------------------------------------------------------------------
1 | package uk.co.activelylazy.selenium;
2 |
3 | import org.openqa.selenium.WebDriver;
4 | import org.openqa.selenium.WebElement;
5 | import org.openqa.selenium.support.FindBy;
6 |
7 | public class AmazonSearchResultsPage {
8 |
9 | private WebDriver driver;
10 |
11 | @FindBy(css="#result_0 .title a")
12 | private WebElement topResultTitle;
13 |
14 | public AmazonSearchResultsPage(WebDriver driver) {
15 | this.driver = driver;
16 | }
17 |
18 | public String getTopResultTitle() {
19 | return topResultTitle.getText();
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/test/java/uk/co/activelylazy/selenium/BetterAmazonSearchTest.java:
--------------------------------------------------------------------------------
1 | package uk.co.activelylazy.selenium;
2 |
3 | import static org.hamcrest.Matchers.is;
4 | import static org.junit.Assert.assertThat;
5 |
6 | import org.junit.After;
7 | import org.junit.Before;
8 | import org.junit.Test;
9 | import org.openqa.selenium.firefox.FirefoxDriver;
10 |
11 | public class BetterAmazonSearchTest {
12 |
13 | private FirefoxDriver driver;
14 |
15 | @Before
16 | public void setupSelenium() {
17 | driver = new FirefoxDriver();
18 | }
19 |
20 | @After
21 | public void closeSelenium() {
22 | driver.close();
23 | driver.quit();
24 | }
25 |
26 | @Test public void
27 | search_amazon() {
28 | AmazonHomePage homePage = AmazonHomePage.navigateTo(driver);
29 | AmazonSearchResultsPage resultsPage = homePage.searchFor("iain banks");
30 | assertThat(resultsPage.getTopResultTitle(), is("Stonemouth"));
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/test/java/uk/co/activelylazy/selenium/AmazonHomePage.java:
--------------------------------------------------------------------------------
1 | package uk.co.activelylazy.selenium;
2 |
3 | import org.openqa.selenium.WebDriver;
4 | import org.openqa.selenium.WebElement;
5 | import org.openqa.selenium.support.FindBy;
6 | import org.openqa.selenium.support.PageFactory;
7 |
8 | public class AmazonHomePage {
9 |
10 | private WebDriver driver;
11 |
12 | @FindBy(name="field-keywords")
13 | private WebElement keywordsField;
14 |
15 | @FindBy(css="#navGoButton input")
16 | private WebElement goButton;
17 |
18 | public AmazonHomePage(WebDriver driver) {
19 | this.driver = driver;
20 | }
21 |
22 | public AmazonSearchResultsPage searchFor(String searchTerm) {
23 | keywordsField.sendKeys(searchTerm);
24 | goButton.click();
25 |
26 | return PageFactory.initElements(driver, AmazonSearchResultsPage.class);
27 | }
28 |
29 | public static AmazonHomePage navigateTo(WebDriver driver) {
30 | driver.get("http://www.amazon.co.uk/");
31 | return PageFactory.initElements(driver, AmazonHomePage.class);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/test/java/uk/co/activelylazy/selenium/SimpleAmazonSearchTest.java:
--------------------------------------------------------------------------------
1 | package uk.co.activelylazy.selenium;
2 |
3 | import static org.hamcrest.Matchers.is;
4 | import static org.junit.Assert.assertThat;
5 |
6 | import org.junit.After;
7 | import org.junit.Before;
8 | import org.junit.Test;
9 | import org.openqa.selenium.By;
10 | import org.openqa.selenium.WebElement;
11 | import org.openqa.selenium.firefox.FirefoxDriver;
12 |
13 | public class SimpleAmazonSearchTest {
14 |
15 | private FirefoxDriver driver;
16 |
17 | @Before
18 | public void setupSelenium() {
19 | driver = new FirefoxDriver();
20 | }
21 |
22 | @After
23 | public void closeSelenium() {
24 | driver.close();
25 | driver.quit();
26 | }
27 |
28 | @Test public void
29 | search_amazon() {
30 | // Open the amazon home page
31 | driver.get("http://www.amazon.co.uk/");
32 |
33 | // Enter a search term
34 | WebElement keywordsField = driver.findElement(By.name("field-keywords"));
35 | keywordsField.sendKeys("iain banks");
36 |
37 | // Click go
38 | WebElement goButton = driver.findElement(By.cssSelector("#navGoButton input"));
39 | goButton.click();
40 |
41 | // Confirm top result
42 | WebElement topResultTitle = driver.findElement(By.cssSelector("#result_0 .title a"));
43 | assertThat(topResultTitle.getText(), is("Stonemouth"));
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
4 | 4.0.0
5 | uk.co.activelylazy.selenium
6 | seleniumguide
7 | jar
8 | 1.0
9 | seleniumguide
10 | http://maven.apache.org
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 2.0.2
17 |
18 | 1.6
19 | 1.6
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 | junit
29 | junit
30 | 4.9
31 | test
32 |
33 |
34 |
35 | org.seleniumhq.selenium
36 | selenium-java
37 | 2.17.0
38 |
39 |
40 |
41 | org.hamcrest
42 | hamcrest-core
43 | 1.2
44 |
45 |
46 |
47 | org.hamcrest
48 | hamcrest-library
49 | 1.2
50 |
51 |
52 |
53 |
54 |
55 | maven2-repository.dev.java.net
56 |
57 | Java.net Repository for Maven
58 |
59 |
60 | http://download.java.net/maven/2
61 |
62 |
63 |
64 |
65 |
66 | xemacs.org.pub.mirrors
67 |
68 | Java Repository for Maven
69 |
70 |
71 | http://ftp.us.xemacs.org/pub/mirrors/maven2
72 |
73 |
74 |
75 |
76 | OCPSOFT
77 | Ocp soft maven repo
78 | http://ocpsoft.com/repository/com/ocpsoft//
79 |
80 |
81 |
82 | prime-repo
83 | Prime Technology Maven Repository
84 | http://repository.prime.com.tr
85 | default
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
--------------------------------------------------------------------------------
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------