Prerequisites:
Java Concepts
1. Parameterized functions,
2. Return Statement,
3. Local and global variable,
4. Constructors, &
5. Static vs non-static methods.
Provide review if needed.
WebDriver driver;
public void openChrome() {
System.setProperty("webdriver.chrome.driver", "c:\\auto\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
public void closeAndQuit() {
driver.close();
driver.quit();
}
public void click(By by) {
driver.findElement(by).click();
}
public void type(By by, String string) {
driver.findElement(by).sendKeys(string);
}
public void compareToReport(String eTitle, String aTitle) {
if (eTitle.equalsIgnoreCase(aTitle)) {
System.out.println(" TEST PASSED!! Expected is : " + eTitle + " , but actual was: " + aTitle);
} else {
System.out.println(" TEST FAILED!! Expected is : " + eTitle + " , but actual was: " + aTitle);
}
}
Java Concepts
1. Parameterized functions,
2. Return Statement,
3. Local and global variable,
4. Constructors, &
5. Static vs non-static methods.
Provide review if needed.
WebDriver driver;
public void openChrome() {
System.setProperty("webdriver.chrome.driver", "c:\\auto\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
public void closeAndQuit() {
driver.close();
driver.quit();
}
public void click(By by) {
driver.findElement(by).click();
}
public void type(By by, String string) {
driver.findElement(by).sendKeys(string);
}
public void compareToReport(String eTitle, String aTitle) {
if (eTitle.equalsIgnoreCase(aTitle)) {
System.out.println(" TEST PASSED!! Expected is : " + eTitle + " , but actual was: " + aTitle);
} else {
System.out.println(" TEST FAILED!! Expected is : " + eTitle + " , but actual was: " + aTitle);
}
}
Test Case: Yahoo Search:
@Test
public void searchForXpathInYahoo() {
System.setProperty("webdriver.chrome.driver", "c:\\auto\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.yahoo.com/");
driver.findElement(By.cssSelector("input#uh-search-box")).sendKeys("xpath");
driver.findElement(By.cssSelector("button[title='Search Web']")).click();
driver.findElement(By.linkText("XPath Tutorial - W3Schools")).click();
System.out.println(driver.getWindowHandles());
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
String eTitle = "Facebook";
String aTitle = driver.getTitle();
if (eTitle.equalsIgnoreCase(aTitle)) {
System.out.println(" Test Passed!! " + "Because expected title was :" + eTitle + "and actual Title is :"
+ aTitle + " The Matched!!");
} else {
System.out.println(" Test Failed!! " + "Because expected title was :" + eTitle + "and actual Title is :"
+ aTitle + " Don't Matched!!");
}
driver.close();
driver.quit();
}
Something Else
private static final By SEARCH_FIELD_LOCATOR = By.id("uh-search-box");
private static final By SEARCH_BUTTON_LOCATOR = By.cssSelector("button[type=submit]");
private static final By W3C_SITE_LOCATOR = By.linkText("XPath Tutorial - W3Schools");
@Test
public void yahooSearchTest() {
openChrome();
driver.get("https://www.yahoo.com/");
type(SEARCH_FIELD_LOCATOR, "xpath");
click(SEARCH_BUTTON_LOCATOR);
click(W3C_SITE_LOCATOR);
System.out.println(driver.getCurrentUrl());
System.out.println(driver.getTitle());
System.out.println(driver.getWindowHandles());
for (String windowHandle : driver.getWindowHandles()) {
driver.switchTo().window(windowHandle);
}
System.out.println(driver.getCurrentUrl());
System.out.println(driver.getTitle());
String eTitle = "XPath Tutorial";
String aTitle = driver.getTitle();
compareToReport(eTitle, aTitle);
closeAndQuit();
}
@Test
public void searchForXpathInYahoo() {
System.setProperty("webdriver.chrome.driver", "c:\\auto\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.yahoo.com/");
driver.findElement(By.cssSelector("input#uh-search-box")).sendKeys("xpath");
driver.findElement(By.cssSelector("button[title='Search Web']")).click();
driver.findElement(By.linkText("XPath Tutorial - W3Schools")).click();
System.out.println(driver.getWindowHandles());
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
String eTitle = "Facebook";
String aTitle = driver.getTitle();
if (eTitle.equalsIgnoreCase(aTitle)) {
System.out.println(" Test Passed!! " + "Because expected title was :" + eTitle + "and actual Title is :"
+ aTitle + " The Matched!!");
} else {
System.out.println(" Test Failed!! " + "Because expected title was :" + eTitle + "and actual Title is :"
+ aTitle + " Don't Matched!!");
}
driver.close();
driver.quit();
}
Something Else
private static final By SEARCH_FIELD_LOCATOR = By.id("uh-search-box");
private static final By SEARCH_BUTTON_LOCATOR = By.cssSelector("button[type=submit]");
private static final By W3C_SITE_LOCATOR = By.linkText("XPath Tutorial - W3Schools");
@Test
public void yahooSearchTest() {
openChrome();
driver.get("https://www.yahoo.com/");
type(SEARCH_FIELD_LOCATOR, "xpath");
click(SEARCH_BUTTON_LOCATOR);
click(W3C_SITE_LOCATOR);
System.out.println(driver.getCurrentUrl());
System.out.println(driver.getTitle());
System.out.println(driver.getWindowHandles());
for (String windowHandle : driver.getWindowHandles()) {
driver.switchTo().window(windowHandle);
}
System.out.println(driver.getCurrentUrl());
System.out.println(driver.getTitle());
String eTitle = "XPath Tutorial";
String aTitle = driver.getTitle();
compareToReport(eTitle, aTitle);
closeAndQuit();
}
Test Case: Add Deposit TechFios CRM Site
// Element Library
private static final String PASSWORD = "abc123";
private static By USERNAME_FIELD_LOCATOR = By.xpath("//input[@id='username']");
// Test Case method starts here
@Test
public void addDeposit() throws InterruptedException {
// Method in base page to open the browser
openChrome();
driver.get("http://techfios.com/test/billing/?ng=admin/"); // goes to the site
// Login Page - enter user name, password, and click submit
driver.findElement(USERNAME_FIELD_LOCATOR).sendKeys("[email protected]");
driver.findElement(By.xpath("//input[@name='password']")).sendKeys(PASSWORD);
driver.findElement(By.xpath("//button[@type='submit']")).click();
// Dashboard Page - click on the add deposit button
driver.findElement(By.linkText("Add Deposit")).click();
// Add Deposit Page - Select an account
driver.findElement(By.xpath("//select[@name='account']/option[contains(@value, 'Ted')]")).click();
Random rnd = new Random(); // Instantiate Random class
String expectedDescription = "Lunch" + rnd.nextInt(100); // Set and store a random description
String amount = String.valueOf(rnd.nextInt(1000)); // Set and store a random amount
driver.findElement(By.id("description")).sendKeys(expectedDescription); // Enter description
driver.findElement(By.id("amount")).sendKeys(amount); // Enter amount
driver.findElement(By.id("submit")).click(); // click submit
Thread.sleep(5000); // wait 5 secs
String actualDescription = driver.findElement(By.linkText(expectedDescription)).getText(); // Get the actual
// description
// Compare expected with actual to pass or fail
if (expectedDescription.equalsIgnoreCase(actualDescription)) {
System.out.println("Test Pass!");
} else {
System.out.println("Test Fail!");
}
driver.close(); // Close all windows
driver.quit(); // Quit the driver
}
// Element Library
private static final String PASSWORD = "abc123";
private static By USERNAME_FIELD_LOCATOR = By.xpath("//input[@id='username']");
// Test Case method starts here
@Test
public void addDeposit() throws InterruptedException {
// Method in base page to open the browser
openChrome();
driver.get("http://techfios.com/test/billing/?ng=admin/"); // goes to the site
// Login Page - enter user name, password, and click submit
driver.findElement(USERNAME_FIELD_LOCATOR).sendKeys("[email protected]");
driver.findElement(By.xpath("//input[@name='password']")).sendKeys(PASSWORD);
driver.findElement(By.xpath("//button[@type='submit']")).click();
// Dashboard Page - click on the add deposit button
driver.findElement(By.linkText("Add Deposit")).click();
// Add Deposit Page - Select an account
driver.findElement(By.xpath("//select[@name='account']/option[contains(@value, 'Ted')]")).click();
Random rnd = new Random(); // Instantiate Random class
String expectedDescription = "Lunch" + rnd.nextInt(100); // Set and store a random description
String amount = String.valueOf(rnd.nextInt(1000)); // Set and store a random amount
driver.findElement(By.id("description")).sendKeys(expectedDescription); // Enter description
driver.findElement(By.id("amount")).sendKeys(amount); // Enter amount
driver.findElement(By.id("submit")).click(); // click submit
Thread.sleep(5000); // wait 5 secs
String actualDescription = driver.findElement(By.linkText(expectedDescription)).getText(); // Get the actual
// description
// Compare expected with actual to pass or fail
if (expectedDescription.equalsIgnoreCase(actualDescription)) {
System.out.println("Test Pass!");
} else {
System.out.println("Test Fail!");
}
driver.close(); // Close all windows
driver.quit(); // Quit the driver
}