Session 4
Objectives
- Q&A
- Alert/Popup
- iFrame
- Hover Over
- Scroll Up/Down
- New Window
- Set and Advance For loop
- Keyboard Action (Keys.ENTER etc.)
- Test Case: Products & Services -> Products: Add/edit/delete product
Alert/Popup
Alert is a small message box which displays on-screen notification to give the user some kind of information or ask for permission to perform certain kind of operation. It may be also used for warning purpose. https://mail.rediff.com/cgi-bin/login.cgi and http://demo.guru99.com/test/delete_customer.php
1) To click on the 'Cancel' button of the alert.
driver.switchTo().alert().dismiss();
2) To click on the 'OK' button of the alert.
driver.switchTo().alert().accept();
3) To capture the alert message.
driver.switchTo().alert().getText();
4) To send some data to alert box.
driver.switchTo().alert().sendKeys("Text");
1) To click on the 'Cancel' button of the alert.
driver.switchTo().alert().dismiss();
2) To click on the 'OK' button of the alert.
driver.switchTo().alert().accept();
3) To capture the alert message.
driver.switchTo().alert().getText();
4) To send some data to alert box.
driver.switchTo().alert().sendKeys("Text");
iFrame
IFrame is a web page which is embedded in another web page or an HTML document embedded inside another HTML document. https://docs.oracle.com/javase/8/docs/api/
Three ways to determine iFrame presence:
Three ways to determine iFrame presence:
- Right click on the element, If you find the option like 'This Frame' then it is an iframe.
- Int size = driver.findElements(By.tagName("iframe")).size(); //Will provide total number of frames
- Inspect the DOM for tag name "frame".
- By Index - driver.switchTo().frame(0);
- By Name or Id - driver.switchTo().frame("iframe1"); or driver.switchTo().frame("id of the element");
- By Web Element - driver.switchTo().frame(WebElement);
- driver.switchTo().parentFrame();
- driver.switchTo().defaultContent();
Mouse Over
Action class is needed when executing mouse over. Example, https://www.dell.com/en-us
Actions action= new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Financing"))).build().perform();
Other Actions:
action.clickAndHold() - Clicks (without releasing) at the current mouse location.
action.doubleClick() - Performs a double-click at the current mouse location.
action.dragAndDrop(source, target) - Performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.
action.dragAndDropBy(source, x-offset, y-offset) - Performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.
action.moveToElement(toElement) - Moves the mouse to the middle of the element.
action.release() - Releases the depressed left mouse button at the current mouse location
Actions action= new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Financing"))).build().perform();
Other Actions:
action.clickAndHold() - Clicks (without releasing) at the current mouse location.
action.doubleClick() - Performs a double-click at the current mouse location.
action.dragAndDrop(source, target) - Performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.
action.dragAndDropBy(source, x-offset, y-offset) - Performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.
action.moveToElement(toElement) - Moves the mouse to the middle of the element.
action.release() - Releases the depressed left mouse button at the current mouse location
Scroll Up/Down
It is used to move the window up and down. Use JavascriptExecutor. Selenium supports javaScriptExecutor. There is no need for an extra plugin or add-on. You just need to import (org.openqa.selenium.JavascriptExecutor) in the script as to use JavaScriptExecutor.
Code:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("scroll(0,400)");
js.executeScript("window.scrollBy(0,1000)");
or
((JavascriptExecutor)getDriver()).executeScript("scroll(0,400)");
Code:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("scroll(0,400)");
js.executeScript("window.scrollBy(0,1000)");
or
((JavascriptExecutor)getDriver()).executeScript("scroll(0,400)");
Handling New Tab/Window:
Driver needs to be switched to a new window in order to access that window. Go to google, type xpath, select link to navigate to w3school site, and click on SQL.
System.out.println(driver.getWindowHandles()); (Optional)
for (String handle: driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
Test Case: Search "xpath" in Yahoo
a. Go to Yahoo.com,
b. Type "xpath" in the search field,
c. Click on the search icon,
d. click on the link that says, "XPath Tutorial - W3Schools",
e. W3Schools page should open (Validation: if else)
System.out.println(driver.getWindowHandles()); (Optional)
for (String handle: driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
Test Case: Search "xpath" in Yahoo
a. Go to Yahoo.com,
b. Type "xpath" in the search field,
c. Click on the search icon,
d. click on the link that says, "XPath Tutorial - W3Schools",
e. W3Schools page should open (Validation: if else)
Class Notes
Automation Example
Handle Popup
@Test
public void handlePopUp() 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);
// Go to Rediff Site
// Click on the go button
// Close the popup
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
driver.findElement(By.xpath("//input[@name='FormName']/following-sibling::div/descendant::input[@value='Go']")).click();
Thread.sleep(3000);
driver.switchTo().alert().accept();
}
Handle iFrame
@Test
public void handleiFrames() {
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);
// Go to the Oracle site
// click on the java.awt.color link
// Click on the ColorSpace link
// Click on the Serializable link
// validate "Interface Serializable" showed
driver.get("https://docs.oracle.com/javase/8/docs/api/");
// switch by index
// driver.switchTo().frame(0);
// switch by name
driver.switchTo().frame("packageListFrame");
driver.findElement(By.linkText("java.awt.color")).click();
// move the driver to the default location
driver.switchTo().defaultContent();
// switch to frame #2 with index
driver.switchTo().frame(1);
// explicit wait on the following element before lick
waitForElement(driver, 40, By.linkText("ColorSpace"));
driver.findElement(By.linkText("ColorSpace")).click();
driver.switchTo().defaultContent();
driver.switchTo().frame(2);
driver.findElement(By.linkText("Serializable")).click();
}
//Explicit wait encapsulated in a method
private void waitForElement(WebDriver driver, int timeToWaitInSeconds, By ElementLocator) {
WebDriverWait wait = new WebDriverWait(driver, timeToWaitInSeconds);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(ElementLocator));
}
Handle Mouse Over
@Test
public void handleMouseOver() {
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);
// Task
// Go to https://www.dell.com/en-us?~ck=mn
// Hover Over Products
// Click on WorkStations
driver.get("https://www.dell.com/en-us?~ck=mn");
// Use Actions class to hover over something
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Products"))).build().perform();
waitForElement(driver, 15, By.linkText("Workstations"));
driver.findElement(By.linkText("Workstations")).click();
waitForElement(driver, 15, By.xpath("//h1[contains(text(),'Precdfgdfision') and contains(text(), 'Workstation')]"));
}
// Explicit wait encapsulated in a method
private void waitForElement(WebDriver driver, int timeToWaitInSeconds, By ElementLocator) {
WebDriverWait wait = new WebDriverWait(driver, timeToWaitInSeconds);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(ElementLocator));
}
Scroll Up and down
@Test
public void scrollUpOrDown() 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(10, TimeUnit.SECONDS);
//Actions action = new Actions(driver);
driver.get("https://www.yahoo.com/");
scrollDown(driver);
Thread.sleep(2000);
scrollUp(driver);
}
private void scrollDown(WebDriver a) {
((JavascriptExecutor)a).executeScript("scroll(0,2000)");
}
private void scrollUp(WebDriver a) {
JavascriptExecutor js = (JavascriptExecutor)a;
js.executeScript("scroll(2000,0)");
}
Handle New Window
@Test
public void handleNewWindowOrTab() 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(10, TimeUnit.SECONDS);
driver.get("https://www.yahoo.com/");
driver.findElement(By.id("uh-search-box")).sendKeys("xpath");
driver.findElement(By.id("uh-search-button")).click();
Thread.sleep(2000);
driver.findElement(By.linkText("XPath Tutorial - w3schools.com")).click();
//This will print all the window handles (which are unique numbers)
System.out.println(driver.getWindowHandles());
//To loop around and end in the last window
for (String windowHandle : driver.getWindowHandles()) {
driver.switchTo().window(windowHandle);
}
Thread.sleep(2000);
driver.findElement(By.linkText("SQL")).click();
}
Test Case : Products & Services -> Products - Add/edit/delete product
@Test
public void handlePopUp() 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);
// Go to Rediff Site
// Click on the go button
// Close the popup
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
driver.findElement(By.xpath("//input[@name='FormName']/following-sibling::div/descendant::input[@value='Go']")).click();
Thread.sleep(3000);
driver.switchTo().alert().accept();
}
Handle iFrame
@Test
public void handleiFrames() {
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);
// Go to the Oracle site
// click on the java.awt.color link
// Click on the ColorSpace link
// Click on the Serializable link
// validate "Interface Serializable" showed
driver.get("https://docs.oracle.com/javase/8/docs/api/");
// switch by index
// driver.switchTo().frame(0);
// switch by name
driver.switchTo().frame("packageListFrame");
driver.findElement(By.linkText("java.awt.color")).click();
// move the driver to the default location
driver.switchTo().defaultContent();
// switch to frame #2 with index
driver.switchTo().frame(1);
// explicit wait on the following element before lick
waitForElement(driver, 40, By.linkText("ColorSpace"));
driver.findElement(By.linkText("ColorSpace")).click();
driver.switchTo().defaultContent();
driver.switchTo().frame(2);
driver.findElement(By.linkText("Serializable")).click();
}
//Explicit wait encapsulated in a method
private void waitForElement(WebDriver driver, int timeToWaitInSeconds, By ElementLocator) {
WebDriverWait wait = new WebDriverWait(driver, timeToWaitInSeconds);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(ElementLocator));
}
Handle Mouse Over
@Test
public void handleMouseOver() {
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);
// Task
// Go to https://www.dell.com/en-us?~ck=mn
// Hover Over Products
// Click on WorkStations
driver.get("https://www.dell.com/en-us?~ck=mn");
// Use Actions class to hover over something
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Products"))).build().perform();
waitForElement(driver, 15, By.linkText("Workstations"));
driver.findElement(By.linkText("Workstations")).click();
waitForElement(driver, 15, By.xpath("//h1[contains(text(),'Precdfgdfision') and contains(text(), 'Workstation')]"));
}
// Explicit wait encapsulated in a method
private void waitForElement(WebDriver driver, int timeToWaitInSeconds, By ElementLocator) {
WebDriverWait wait = new WebDriverWait(driver, timeToWaitInSeconds);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(ElementLocator));
}
Scroll Up and down
@Test
public void scrollUpOrDown() 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(10, TimeUnit.SECONDS);
//Actions action = new Actions(driver);
driver.get("https://www.yahoo.com/");
scrollDown(driver);
Thread.sleep(2000);
scrollUp(driver);
}
private void scrollDown(WebDriver a) {
((JavascriptExecutor)a).executeScript("scroll(0,2000)");
}
private void scrollUp(WebDriver a) {
JavascriptExecutor js = (JavascriptExecutor)a;
js.executeScript("scroll(2000,0)");
}
Handle New Window
@Test
public void handleNewWindowOrTab() 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(10, TimeUnit.SECONDS);
driver.get("https://www.yahoo.com/");
driver.findElement(By.id("uh-search-box")).sendKeys("xpath");
driver.findElement(By.id("uh-search-button")).click();
Thread.sleep(2000);
driver.findElement(By.linkText("XPath Tutorial - w3schools.com")).click();
//This will print all the window handles (which are unique numbers)
System.out.println(driver.getWindowHandles());
//To loop around and end in the last window
for (String windowHandle : driver.getWindowHandles()) {
driver.switchTo().window(windowHandle);
}
Thread.sleep(2000);
driver.findElement(By.linkText("SQL")).click();
}
Test Case : Products & Services -> Products - Add/edit/delete product
Homework
Automate the following test case:
Scenario: CRM -> Add Contact - Add contact and validate contact added in List Contacts
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 CRM in the Side Navigation
6. Click on Add Contact
7. Fill in the Add Contact Form,
8. Click submit,
9. Go to CRM -> List Contact Page
10. Search for the new contact in the search field,
12. Validate contact created.
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.