Skip to content

Commit

Permalink
Added latest code examples for all the chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
upgundecha committed Oct 27, 2015
1 parent c207d39 commit 915b897
Show file tree
Hide file tree
Showing 72 changed files with 3,913 additions and 45 deletions.
69 changes: 52 additions & 17 deletions java/pom.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.secookbook.examples</groupId>
<artifactId>SeleniumCookbook</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.secookbook.examples</groupId>
<artifactId>SeleniumCookbook</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
<scope>test</scope>
</dependency>
<!-- <dependency> <groupId>com.github.detro</groupId> <artifactId>phantomjsdriver</artifactId>
<version>1.2.0</version> <scope>test</scope> </dependency> -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ public class GoogleSearchTest {

@Before
public void setUp() {
// launch a new Firefox instance
// Launch a new Firefox instance
driver = new FirefoxDriver();
// maximize the browser window
// Maximize the browser window
driver.manage().window().maximize();
// navigate to Google
// Navigate to Google
driver.get("http://www.google.com");
}

@Test
public void testGoogleSearch() {
// find the text input element by its name
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// clear the existing text value
// Clear the existing text value
element.clear();

// enter something to search for
// Enter something to search for
element.sendKeys("Selenium testing tools cookbook");

// now submit the form
// Now submit the form
element.submit();

// Google's search is rendered dynamically with JavaScript.
// wait for the page to load, timeout after 10 seconds
// Wait for the page to load, timeout after 10 seconds
new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase()
Expand All @@ -52,7 +52,7 @@ public Boolean apply(WebDriver d) {

@After
public void tearDown() throws Exception {
// close the browser
// Close the browser
driver.quit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ public class GoogleSearchTestOnChrome {
@Before
public void setUp() {
System.setProperty("webdriver.chrome.driver",
"src/test/resources/drivers/chromedriver.exe");
// launch Chrome
"./src/test/resources/drivers/chromedriver.exe");

// Launch Chrome
driver = new ChromeDriver();
// maximize the browser window
// Maximize the browser window
driver.manage().window().maximize();
// navigate to Google
// Navigate to Google
driver.get("http://www.google.com");
}

@Test
public void testGoogleSearch() {
// find the text input element by its name
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));

// enter something to search for
// Enter something to search for
element.sendKeys("Selenium testing tools cookbook");

// now submit the form. WebDriver will find
// Now submit the form. WebDriver will find
// the form for us from the element
element.submit();

// Google's search is rendered dynamically with JavaScript.
// wait for the page to load, timeout after 10 seconds
// Wait for the page to load, timeout after 10 seconds
new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase()
Expand All @@ -54,7 +54,7 @@ public Boolean apply(WebDriver d) {

@After
public void tearDown() throws Exception {
// close the browser
// Close the browser
driver.quit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.secookbook.examples.chapter01;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class GoogleSearchTestOnEdge {

private WebDriver driver;

@Before
public void setUp() {
System.setProperty("webdriver.edge.driver",
"C:\\Program Files (x86)\\Microsoft Web Driver\\MicrosoftWebDriver.exe");

EdgeOptions options = new EdgeOptions();
options.setPageLoadStrategy("eager");

// Launch a new Edge instance
driver = new EdgeDriver(options);

// Navigate to Google
driver.get("http://www.google.com");
}

@Test
public void testGoogleSearch() {
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));

// Clear the existing text value
element.clear();

// Enter something to search for
element.sendKeys("Selenium testing tools cookbook");

WebElement button = driver.findElement(By.name("btnG"));
button.click();

// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase()
.startsWith("selenium testing tools cookbook");
}
});

assertEquals("Selenium testing tools cookbook - Google Search",
driver.getTitle());
}

@After
public void tearDown() throws Exception {
// Close the browser
driver.quit();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,34 @@ public void setUp() {
System.setProperty("webdriver.ie.driver",
"src/test/resources/drivers/IEDriverServer.exe");

DesiredCapabilities caps = new DesiredCapabilities().internetExplorer();
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();

caps.setCapability(
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);

// launch Internet Explorer
// Launch Internet Explorer
driver = new InternetExplorerDriver(caps);
// maximize the browser window
// Maximize the browser window
driver.manage().window().maximize();
// navigate to Google
// Navigate to Google
driver.get("http://www.google.com");
}

@Test
public void testGoogleSearch() {
// find the text input element by its name
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));

// enter something to search for
// Enter something to search for
element.sendKeys("Selenium testing tools cookbook");

// now submit the form. WebDriver will find
// Now submit the form. WebDriver will find
// the form for us from the element
element.submit();

// Google's search is rendered dynamically with JavaScript.
// wait for the page to load, timeout after 10 seconds
// Wait for the page to load, timeout after 10 seconds
new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase()
Expand All @@ -61,7 +61,7 @@ public Boolean apply(WebDriver d) {

@After
public void tearDown() throws Exception {
// close the browser
// Close the browser
driver.quit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.secookbook.examples.chapter02;

import java.util.Arrays;
import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.JavascriptExecutor;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class JQuerySelector {

protected WebDriver driver;

@Before
public void setUp() {
driver = new ChromeDriver();
driver.get("http://http://cookbook.seleniumacademy.com/Locators.html");
}

@Test
public void testDefaultSelectedCheckbox() {

// Expected list of selected Checkbox
List<String> checked = Arrays
.asList("user128_admin", "user220_browser");

// Create an instance of JavaScript Executor from driver
JavascriptExecutor js = (JavascriptExecutor) driver;

// Locate all the Checkbox which are checked by calling jQuery find()
// method.
// find() method returns elements in array
@SuppressWarnings("unchecked")
List<WebElement> elements = (List<WebElement>) js
.executeScript("return jQuery.find(':checked')");

// Verify two Checkbox are selected
assertEquals(elements.size(), 2);

// Verify correct Checkbox are selected
for (WebElement element : elements) {
assertTrue(checked.contains(element.getAttribute("id")));
}
}

@After
public void tearDown() {
driver.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.secookbook.examples.chapter03;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

public class CheckBoxTest {

private WebDriver driver;

@Before
public void setUp() {
driver = new FirefoxDriver();
driver.get("http://cookbook.seleniumacademy.com/Config.html");
}

@Test
public void testCheckBox() {
//Get the Checkbox as WebElement using it's value attribute
WebElement airbags = driver.findElement(By.xpath("//input[@value='Airbags']"));

//Check if its already selected? otherwise select the Checkbox
//by calling click() method
if (!airbags.isSelected()) {
airbags.click();
}

//Verify Checkbox is Selected
assertTrue(airbags.isSelected());

//Check Checkbox if selected? If yes, deselect it
//by calling click() method
if (airbags.isSelected()) {
airbags.click();
}

//Verify Checkbox is Deselected
assertFalse(airbags.isSelected());
}

@After
public void tearDown() {
driver.quit();
}
}
Loading

0 comments on commit 915b897

Please sign in to comment.