-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added latest code examples for all the chapters
- Loading branch information
1 parent
c207d39
commit 915b897
Showing
72 changed files
with
3,913 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
java/src/test/java/com/secookbook/examples/chapter01/GoogleSearchTestOnEdge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
java/src/test/java/com/secookbook/examples/chapter02/JQuerySelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
java/src/test/java/com/secookbook/examples/chapter03/CheckBoxTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.