Session 3
Objectives
- Q&A
- Xpath Axes
- Explicit Wait
- Select Class
- Assertion
- try/catch
Xpath Axes
Syntax: /relationship::
a) Following: Goes to any node after a the current node. Example,
//*[@type='text']/following::input
//*[@type='text']/following::input[1]
b) Ancestor: Goes to any node before a the current node. Example,
//input[@name='password']/ancestor::table
c) Child: Goes to all children nodes within the current node. Example,
//form[@name="frmLogin"]/child::table
d) Preceding: Goes to all nodes that come before the current node. Example,
//input[@type='password']/preceding::input
e) Following-sibling/preceding-sibling: Goes to all nodes that come after the current node on the same level. Example,
//input[@type='password']/following-sibling::label
f) Parent: Goes to the immediate parent node of the current node. Example,
//input[@type='password']/parent::td
f) Descendant: Goes far down the line of the current node. Example,
//div[@class="container-fluid"]/descendant::a
Explicit Wait
"stated clearly and in detail, leaving no room for confusion or doubt." - The explicit wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an "ElementNotVisibleException" exception. The explicit wait is an intelligent kind of wait can be applied only for specified elements. Used to check if page loaded. Example,
WebDriverWait wait = new WebDriverWait(driver, 60); //time in secs
wait.until(ExpectedConditions.visibilityOfElementLocated(By Locator));
Common Expected Conditions:
WebDriverWait wait = new WebDriverWait(driver, 60); //time in secs
wait.until(ExpectedConditions.visibilityOfElementLocated(By Locator));
Common Expected Conditions:
- elementToBeClickable()
- invisibilityOfTheElementLocated()
- presenceOfElementLocated()
- visibilityOfElementLocated()
Class Notes
Automation Example
Test Case 2: Add and verify deposit
@Test
public void userShouldBeAbleToAddDeposit() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
// driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://techfios.com/test/billing/?ng=admin/");
// driver.get("https://www.facebook.com/");
// Element Library
WebElement usernameField = driver.findElement(By.xpath("(//input[@class='form-control'])[1]"));
WebElement passwordField = driver.findElement(By.xpath("//input[contains(@type, 'pass')]"));
WebElement signInButton = driver
.findElement(By.xpath("//*[contains(@class, 'success block') and @type='submit']"));
// WebElement addDepositButton = driver.findElement(By.linkText("Add Deposit"));
By addDepositeButtonLocator = By.linkText("Add Deposit");
By addDepositPanelHeaderLocator = By.xpath("//h5[contains(text(), 'Add Deposit')]");
String expectedTitle = "Login - TechFios Test Application - Billing";
String actualTitle = driver.getTitle();
if (expectedTitle.toLowerCase().contains(actualTitle.toLowerCase())) {
System.out.println("Test Pass" + " Actual Title: " + actualTitle + " and Expected Title: " + expectedTitle
+ " - Matched!!!");
} else {
System.out.println("Test Failed" + " Actual Title: " + actualTitle + " and Expected Title: " + expectedTitle
+ " - Didn't Match!!!");
throw new RuntimeException("Test Failed.");
}
usernameField.sendKeys("[email protected]");
passwordField.sendKeys("abc123");
signInButton.click();
// Explicit Wait
// WebDriverWait wait = new WebDriverWait(driver, 5);
// wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(addDepositeButtonLocator));
// Method to use Explicit wait
waitForElement(driver, 10, addDepositeButtonLocator);
// click on add deposit button
driver.findElement(addDepositeButtonLocator).click();
// Expilictly wait for Panel Header of the Add Deposit Page
waitForElement(driver, 10, addDepositPanelHeaderLocator);
boolean status = false;
try {
status = driver.findElement(addDepositPanelHeaderLocator).isDisplayed();
} catch (NoSuchElementException e) {
status = false;
}
// Assertion to validate whether the Panel Header Displayed
Assert.assertTrue("Add Deposit Page didn't Display", status);
WebElement accountDropDown = driver.findElement(By.id("account"));
// Selecting using Select Class from DropDown
Select select = new Select(accountDropDown);
// select.selectByValue("Rutu");
select.selectByIndex(5);
// Generate Random number
Random rnd = new Random();
int randomNumber = rnd.nextInt(9999);
String description = "Lunch" + randomNumber;
String amount = String.valueOf(randomNumber * 3);
driver.findElement(By.name("description")).sendKeys(description);
driver.findElement(By.name("amount")).sendKeys(amount);
driver.findElement(By.id("submit")).click();
By firstTableDataLocator = By.xpath("//a[contains(text(), '" + description + "')]");
waitForElement(driver, 20, firstTableDataLocator);
System.out.println(description);
System.out.println(driver.findElement(firstTableDataLocator).getText());
assertEquals("Did not deposit!", description, driver.findElement(firstTableDataLocator).getText());
// driver.close();
// driver.quit();
}
private void waitForElement(WebDriver driver, int i, By by) {
WebDriverWait wait = new WebDriverWait(driver, i);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by));
}
@Test
public void userShouldBeAbleToAddDeposit() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
// driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://techfios.com/test/billing/?ng=admin/");
// driver.get("https://www.facebook.com/");
// Element Library
WebElement usernameField = driver.findElement(By.xpath("(//input[@class='form-control'])[1]"));
WebElement passwordField = driver.findElement(By.xpath("//input[contains(@type, 'pass')]"));
WebElement signInButton = driver
.findElement(By.xpath("//*[contains(@class, 'success block') and @type='submit']"));
// WebElement addDepositButton = driver.findElement(By.linkText("Add Deposit"));
By addDepositeButtonLocator = By.linkText("Add Deposit");
By addDepositPanelHeaderLocator = By.xpath("//h5[contains(text(), 'Add Deposit')]");
String expectedTitle = "Login - TechFios Test Application - Billing";
String actualTitle = driver.getTitle();
if (expectedTitle.toLowerCase().contains(actualTitle.toLowerCase())) {
System.out.println("Test Pass" + " Actual Title: " + actualTitle + " and Expected Title: " + expectedTitle
+ " - Matched!!!");
} else {
System.out.println("Test Failed" + " Actual Title: " + actualTitle + " and Expected Title: " + expectedTitle
+ " - Didn't Match!!!");
throw new RuntimeException("Test Failed.");
}
usernameField.sendKeys("[email protected]");
passwordField.sendKeys("abc123");
signInButton.click();
// Explicit Wait
// WebDriverWait wait = new WebDriverWait(driver, 5);
// wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(addDepositeButtonLocator));
// Method to use Explicit wait
waitForElement(driver, 10, addDepositeButtonLocator);
// click on add deposit button
driver.findElement(addDepositeButtonLocator).click();
// Expilictly wait for Panel Header of the Add Deposit Page
waitForElement(driver, 10, addDepositPanelHeaderLocator);
boolean status = false;
try {
status = driver.findElement(addDepositPanelHeaderLocator).isDisplayed();
} catch (NoSuchElementException e) {
status = false;
}
// Assertion to validate whether the Panel Header Displayed
Assert.assertTrue("Add Deposit Page didn't Display", status);
WebElement accountDropDown = driver.findElement(By.id("account"));
// Selecting using Select Class from DropDown
Select select = new Select(accountDropDown);
// select.selectByValue("Rutu");
select.selectByIndex(5);
// Generate Random number
Random rnd = new Random();
int randomNumber = rnd.nextInt(9999);
String description = "Lunch" + randomNumber;
String amount = String.valueOf(randomNumber * 3);
driver.findElement(By.name("description")).sendKeys(description);
driver.findElement(By.name("amount")).sendKeys(amount);
driver.findElement(By.id("submit")).click();
By firstTableDataLocator = By.xpath("//a[contains(text(), '" + description + "')]");
waitForElement(driver, 20, firstTableDataLocator);
System.out.println(description);
System.out.println(driver.findElement(firstTableDataLocator).getText());
assertEquals("Did not deposit!", description, driver.findElement(firstTableDataLocator).getText());
// driver.close();
// driver.quit();
}
private void waitForElement(WebDriver driver, int i, By by) {
WebDriverWait wait = new WebDriverWait(driver, i);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by));
}
Homework
Automate the following test case:
Scenario: Users will be able to Transfer Money
1: Open Browser and go to site http://techfios.com/test/billing/?ng=admin/
2. Enter username: [email protected]
3. Enter password: abc123
4. Click login button
5. Click on Transactions in the Side Navigation
6. Click on transfer
7. Fill in the New Transfer Form,
8. Click submit,
9. Validate transfer posted on the table.
Interview Questions
1. What is the difference between explicit and implicit wait?
2. What is assertion?
3. What are some ways you can use assertion?
4. How can you get a random number?
5. What are the challenges of automation?
6. What are some common exceptions you have experience with?
7. How can you handle exceptions?
2. What is assertion?
3. What are some ways you can use assertion?
4. How can you get a random number?
5. What are the challenges of automation?
6. What are some common exceptions you have experience with?
7. How can you handle exceptions?
Shortcuts/Tricks:
“Ctrl + Shift + F + O” - Align and remove unused imports.
“Ctrl + Alt + Down" - Duplicate the row where is the cursor is.
"Alt + Left" - Navigate back.
"Alt + Right" - Navigate forward.
“Ctrl + Alt + Down" - Duplicate the row where is the cursor is.
"Alt + Left" - Navigate back.
"Alt + Right" - Navigate forward.
Class Code
// Scenario/Test Case: User should be able to add contact
// 1. Open the browser
// 2. Go to the techfios site,
// 3. type user name and password and submit,
// 4. Open CRM,
// 5. click on Add Contact,
// 6. Fill out the form and submit,
// 7. Click on List Contact,
// 8. Verify that Contact added.
@Test
public void userShouldBeAbleToAddContact() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Set Certain properties
// driver.manage().window().maximize();
// driver.manage().deleteAllCookies();
// Implicit Wait, Applies to all, Set it once at the beginning
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// 2. Go to the techfios site,
driver.get("http://techfios.com/test/billing/?ng=admin/");
// 3. type user name and password and submit,
driver.findElement(By.xpath("//input[@placeholder='Email Address']")).sendKeys("[email protected]");
driver.findElement(By.xpath("//input[@type='password']")).sendKeys("abc123");
driver.findElement(By.xpath("//button[contains(text(), 'Sign')]")).click();
// Library Of Element
By PAGE_TITLE_LOCATOR = By.xpath("//h2[contains(text(), 'Dashboard')]");
By CRM_SIDE_NAV_LOCATOR = By.xpath("//ul[@id='side-menu']/descendant::span[text()='CRM']");
By ADD_CONTACT_SIDE_NAV_LOCATOR = By
.xpath("//span[text()='CRM']/parent::*/following-sibling::ul/descendant::a[contains(@href,'add')]");
By FULL_NAME_INPUT_FIELD_LOCATOR = By.id("account");
By COMPANY_NAME_INPUT_FIELD_LOCATOR = By.id("company");
By EMAIL_INPUT_FIELD_LOCATOR = By.id("email");
By PHONE_INPUT_FIELD_LOCATOR = By.id("phone");
By ADDRESS_INPUT_FIELD_LOCATOR = By.id("address");
By CITY_INPUT_FIELD_LOCATOR = By.id("city");
By STATE_INPUT_FIELD_LOCATOR = By.id("state");
By ZIP_INPUT_FIELD_LOCATOR = By.id("zip");
By SUBMIT_BUTTON_LOCATOR = By.id("submit");
By LIST_CONTACT_SIDE_NAV_LOCATOR = By.linkText("List Contacts");
By NEWLY_ADDED_DISPLAYED_CONTACT_LOCATOR = By.xpath("//table/tbody/descendant::tr[1]/td[2]/a");
Random rnd = new Random();
int randomNumber = rnd.nextInt(999);
String fullName = "Deepika" + randomNumber;
String companyName = "Techfios" + randomNumber;
String email = "techfios" + randomNumber + "@gmail.com";
String phone = "1234564" + randomNumber;
String address = randomNumber + " Street";
String city = "Irving" + randomNumber;
String state = "TX";
String zip = "75062";
// Explicit wait given to the driver
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(PAGE_TITLE_LOCATOR));
// 4. Open CRM
driver.findElement(CRM_SIDE_NAV_LOCATOR).click();
waitForElement(driver, 10, ADD_CONTACT_SIDE_NAV_LOCATOR);
// 5. click on Add Contact,
driver.findElement(ADD_CONTACT_SIDE_NAV_LOCATOR).click();
// 6. Fill out the form and submit,
waitForElement(driver, 10, FULL_NAME_INPUT_FIELD_LOCATOR);
driver.findElement(FULL_NAME_INPUT_FIELD_LOCATOR).sendKeys(fullName);
driver.findElement(COMPANY_NAME_INPUT_FIELD_LOCATOR).sendKeys(companyName);
driver.findElement(EMAIL_INPUT_FIELD_LOCATOR).sendKeys(email);
driver.findElement(PHONE_INPUT_FIELD_LOCATOR).sendKeys(phone);
driver.findElement(ADDRESS_INPUT_FIELD_LOCATOR).sendKeys(address);
driver.findElement(CITY_INPUT_FIELD_LOCATOR).sendKeys(city);
driver.findElement(STATE_INPUT_FIELD_LOCATOR).sendKeys(state);
driver.findElement(ZIP_INPUT_FIELD_LOCATOR).sendKeys(zip);
driver.findElement(SUBMIT_BUTTON_LOCATOR).click();
// 7. Click on List Contact,
driver.findElement(LIST_CONTACT_SIDE_NAV_LOCATOR).click();
// 8. Verify that Contact added.
String displayedContactName = driver.findElement(NEWLY_ADDED_DISPLAYED_CONTACT_LOCATOR).getText();
// Print to see
System.out.println("Expected Name" + fullName);
System.out.println("actual Name" + displayedContactName);
Assert.assertEquals("Relevant contact name not found!", fullName, displayedContactName);
// Close the browser
driver.close();
// Ends the WebDriver session
driver.quit();
}
public void waitForElement(WebDriver driver, int timeInSeconds, By locator) {
WebDriverWait wait = new WebDriverWait(driver, timeInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
// 1. Open the browser
// 2. Go to the techfios site,
// 3. type user name and password and submit,
// 4. Open CRM,
// 5. click on Add Contact,
// 6. Fill out the form and submit,
// 7. Click on List Contact,
// 8. Verify that Contact added.
@Test
public void userShouldBeAbleToAddContact() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Set Certain properties
// driver.manage().window().maximize();
// driver.manage().deleteAllCookies();
// Implicit Wait, Applies to all, Set it once at the beginning
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// 2. Go to the techfios site,
driver.get("http://techfios.com/test/billing/?ng=admin/");
// 3. type user name and password and submit,
driver.findElement(By.xpath("//input[@placeholder='Email Address']")).sendKeys("[email protected]");
driver.findElement(By.xpath("//input[@type='password']")).sendKeys("abc123");
driver.findElement(By.xpath("//button[contains(text(), 'Sign')]")).click();
// Library Of Element
By PAGE_TITLE_LOCATOR = By.xpath("//h2[contains(text(), 'Dashboard')]");
By CRM_SIDE_NAV_LOCATOR = By.xpath("//ul[@id='side-menu']/descendant::span[text()='CRM']");
By ADD_CONTACT_SIDE_NAV_LOCATOR = By
.xpath("//span[text()='CRM']/parent::*/following-sibling::ul/descendant::a[contains(@href,'add')]");
By FULL_NAME_INPUT_FIELD_LOCATOR = By.id("account");
By COMPANY_NAME_INPUT_FIELD_LOCATOR = By.id("company");
By EMAIL_INPUT_FIELD_LOCATOR = By.id("email");
By PHONE_INPUT_FIELD_LOCATOR = By.id("phone");
By ADDRESS_INPUT_FIELD_LOCATOR = By.id("address");
By CITY_INPUT_FIELD_LOCATOR = By.id("city");
By STATE_INPUT_FIELD_LOCATOR = By.id("state");
By ZIP_INPUT_FIELD_LOCATOR = By.id("zip");
By SUBMIT_BUTTON_LOCATOR = By.id("submit");
By LIST_CONTACT_SIDE_NAV_LOCATOR = By.linkText("List Contacts");
By NEWLY_ADDED_DISPLAYED_CONTACT_LOCATOR = By.xpath("//table/tbody/descendant::tr[1]/td[2]/a");
Random rnd = new Random();
int randomNumber = rnd.nextInt(999);
String fullName = "Deepika" + randomNumber;
String companyName = "Techfios" + randomNumber;
String email = "techfios" + randomNumber + "@gmail.com";
String phone = "1234564" + randomNumber;
String address = randomNumber + " Street";
String city = "Irving" + randomNumber;
String state = "TX";
String zip = "75062";
// Explicit wait given to the driver
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(PAGE_TITLE_LOCATOR));
// 4. Open CRM
driver.findElement(CRM_SIDE_NAV_LOCATOR).click();
waitForElement(driver, 10, ADD_CONTACT_SIDE_NAV_LOCATOR);
// 5. click on Add Contact,
driver.findElement(ADD_CONTACT_SIDE_NAV_LOCATOR).click();
// 6. Fill out the form and submit,
waitForElement(driver, 10, FULL_NAME_INPUT_FIELD_LOCATOR);
driver.findElement(FULL_NAME_INPUT_FIELD_LOCATOR).sendKeys(fullName);
driver.findElement(COMPANY_NAME_INPUT_FIELD_LOCATOR).sendKeys(companyName);
driver.findElement(EMAIL_INPUT_FIELD_LOCATOR).sendKeys(email);
driver.findElement(PHONE_INPUT_FIELD_LOCATOR).sendKeys(phone);
driver.findElement(ADDRESS_INPUT_FIELD_LOCATOR).sendKeys(address);
driver.findElement(CITY_INPUT_FIELD_LOCATOR).sendKeys(city);
driver.findElement(STATE_INPUT_FIELD_LOCATOR).sendKeys(state);
driver.findElement(ZIP_INPUT_FIELD_LOCATOR).sendKeys(zip);
driver.findElement(SUBMIT_BUTTON_LOCATOR).click();
// 7. Click on List Contact,
driver.findElement(LIST_CONTACT_SIDE_NAV_LOCATOR).click();
// 8. Verify that Contact added.
String displayedContactName = driver.findElement(NEWLY_ADDED_DISPLAYED_CONTACT_LOCATOR).getText();
// Print to see
System.out.println("Expected Name" + fullName);
System.out.println("actual Name" + displayedContactName);
Assert.assertEquals("Relevant contact name not found!", fullName, displayedContactName);
// Close the browser
driver.close();
// Ends the WebDriver session
driver.quit();
}
public void waitForElement(WebDriver driver, int timeInSeconds, By locator) {
WebDriverWait wait = new WebDriverWait(driver, timeInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}