Session 2
Objectives
- Q&A
- Element Locators and Examples (id, name, className, linkText, partialLinkText)
- cssSelector: 1. tag#id, 2. tag.class, 3. tag[attribute='value'], 4. tag.class[attribute='value']
- Xpath (Absolute vs Relative)
- Xpath with text(), contains(), and, or, *
- Implicit Wait vs. Explicit Wait
- isDisplayed(), isEnabled(), isSelected()
- Automate TestCase: TBD
cssSelector
http://objectspy.space/
Benefits: 1. Elements are faster for the driver to identify, & 2. Tags and attributes can be combined to get an unique element.
1. tag#id,
2. tag.class,
3. tag[attribute='value'],
4. tag.class[attribute='value']
1. tag#id,
2. tag.class,
3. tag[attribute='value'],
4. tag.class[attribute='value']
xpath (Absolute vs Relative)
http://objectspy.space/
xpath: and, or, contains(), text(), *
waits
Class Notes
Automation Example
Class Name: VariousLocatorsAndElements
@Test
public void variousLocatorsAndElements() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://objectspy.space/");
Thread.sleep(3000);
//Clear the search field
// driver.findElement(By.className("search")).clear();
// Thread.sleep(3000);
//
// //Type techfios into search field
// driver.findElement(By.className("search")).sendKeys("Techfios");
// Thread.sleep(3000);
//
// //Examples of cssSelector
// driver.findElement(By.cssSelector("input[title='Search']")).click();
//Click on a linked Text (Display in different different color)
//Will always have a tagname "a" and url
// Thread.sleep(3000);
// driver.findElement(By.linkText("Link Test : New Page")).click();
//Partial Link Text
// driver.findElement(By.partialLinkText("OS-API")).click();
//Absolute xpath example
// driver.findElement(By.xpath("html/body/div/div[2]/div[2]/form/div[4]/a")).click();
//Relative xpath example for radio button Male
// driver.findElement(By.xpath("//input[@value='Male']")).click();
// Thread.sleep(2000);
// driver.findElement(By.xpath("//*[@value='Female']")).click();
//Relative xpath with function text()
// driver.findElement(By.xpath("//strong[text()='Link Test : New Page']")).click();
//Relative xpath with contains()
// driver.findElement(By.xpath("//*[contains(text(), 'New Page')]")).click();
//xpath with "and"
//*[contains(text(), 'New Page') and contains(text(),'Link')]
//label[@class='checkbox' and contains(text(), 'Auto')]
// Thread.sleep(3000);
// //Close the browser
// driver.close();
// //Quite the driver
// driver.quit();
}
Class Name: LoginTest
@Test
public void validUsersShouldBeAbleToLogIn() throws InterruptedException {
//Set Chrome as default browser
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
//Open Chrome Browserd
WebDriver driver = new ChromeDriver();
//Maximize the window
// driver.manage().window().maximize();
//Implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Go to TechFios Website
driver.get("http://techfios.com/test/billing/?ng=admin/");
//Type username in the username field
driver.findElement(By.id("username")).sendKeys("[email protected]");
// Thread.sleep(2000);
//Type password in the password field
driver.findElement(By.name("password")).sendKeys("abc123");
// Thread.sleep(2000);
//Click on Sign In button
driver.findElement(By.name("login")).click();
//Dashboard page should display
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//h2[contains(text(),'Dashboard')]"))));
driver.close();
driver.quit();
}
Test Case: Add and Verify Deposit
@Test
public void addAndVerifyDeposit() throws InterruptedException {
//Set Chrome as default browser
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
//Open Chrome Browserd
WebDriver driver = new ChromeDriver();
//Go to TechFios Website
driver.get("http://techfios.com/test/billing/?ng=admin/");
//Type username in the username field
driver.findElement(By.id("username")).sendKeys("[email protected]");
//Type password in the password field
driver.findElement(By.name("password")).sendKeys("abc123");
//Click on Sign In button
driver.findElement(By.name("login")).click();
driver.findElement(By.linkText("Add Deposit")).click();
// driver.get("http://demo.guru99.com/test/newtours/register.php");
// WebElement element = driver.findElement(By.name("country"));
// Select select = new Select(element);
// select.selectByValue("ANGOLA");
Thread.sleep(3000);
WebElement element = driver.findElement(By.name("account"));
Select select = new Select(element);
select.selectByIndex(8);
//driver.findElement(By.xpath("//select[@id='account']/option[@value='Sarker']")).click();
Random rnd = new Random();
int randomNumber = rnd.nextInt(999);
String description = "Shopping"+randomNumber;
String amount = String.valueOf(randomNumber);
//driver.findElement(By.name("description")).sendKeys(description);
driver.findElement(By.name("amount")).sendKeys(amount);
driver.findElement(By.id("submit")).click();
//Thread.sleep(5000);
By recentDepositLocator = By.linkText(description);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(recentDepositLocator));
boolean status = driver.findElement(recentDepositLocator).isDisplayed();
if(status==true) {
System.out.println("Test Passed");
} else System.out.println("Test Failed!!");
}
@Test
public void variousLocatorsAndElements() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://objectspy.space/");
Thread.sleep(3000);
//Clear the search field
// driver.findElement(By.className("search")).clear();
// Thread.sleep(3000);
//
// //Type techfios into search field
// driver.findElement(By.className("search")).sendKeys("Techfios");
// Thread.sleep(3000);
//
// //Examples of cssSelector
// driver.findElement(By.cssSelector("input[title='Search']")).click();
//Click on a linked Text (Display in different different color)
//Will always have a tagname "a" and url
// Thread.sleep(3000);
// driver.findElement(By.linkText("Link Test : New Page")).click();
//Partial Link Text
// driver.findElement(By.partialLinkText("OS-API")).click();
//Absolute xpath example
// driver.findElement(By.xpath("html/body/div/div[2]/div[2]/form/div[4]/a")).click();
//Relative xpath example for radio button Male
// driver.findElement(By.xpath("//input[@value='Male']")).click();
// Thread.sleep(2000);
// driver.findElement(By.xpath("//*[@value='Female']")).click();
//Relative xpath with function text()
// driver.findElement(By.xpath("//strong[text()='Link Test : New Page']")).click();
//Relative xpath with contains()
// driver.findElement(By.xpath("//*[contains(text(), 'New Page')]")).click();
//xpath with "and"
//*[contains(text(), 'New Page') and contains(text(),'Link')]
//label[@class='checkbox' and contains(text(), 'Auto')]
// Thread.sleep(3000);
// //Close the browser
// driver.close();
// //Quite the driver
// driver.quit();
}
Class Name: LoginTest
@Test
public void validUsersShouldBeAbleToLogIn() throws InterruptedException {
//Set Chrome as default browser
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
//Open Chrome Browserd
WebDriver driver = new ChromeDriver();
//Maximize the window
// driver.manage().window().maximize();
//Implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Go to TechFios Website
driver.get("http://techfios.com/test/billing/?ng=admin/");
//Type username in the username field
driver.findElement(By.id("username")).sendKeys("[email protected]");
// Thread.sleep(2000);
//Type password in the password field
driver.findElement(By.name("password")).sendKeys("abc123");
// Thread.sleep(2000);
//Click on Sign In button
driver.findElement(By.name("login")).click();
//Dashboard page should display
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//h2[contains(text(),'Dashboard')]"))));
driver.close();
driver.quit();
}
Test Case: Add and Verify Deposit
@Test
public void addAndVerifyDeposit() throws InterruptedException {
//Set Chrome as default browser
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
//Open Chrome Browserd
WebDriver driver = new ChromeDriver();
//Go to TechFios Website
driver.get("http://techfios.com/test/billing/?ng=admin/");
//Type username in the username field
driver.findElement(By.id("username")).sendKeys("[email protected]");
//Type password in the password field
driver.findElement(By.name("password")).sendKeys("abc123");
//Click on Sign In button
driver.findElement(By.name("login")).click();
driver.findElement(By.linkText("Add Deposit")).click();
// driver.get("http://demo.guru99.com/test/newtours/register.php");
// WebElement element = driver.findElement(By.name("country"));
// Select select = new Select(element);
// select.selectByValue("ANGOLA");
Thread.sleep(3000);
WebElement element = driver.findElement(By.name("account"));
Select select = new Select(element);
select.selectByIndex(8);
//driver.findElement(By.xpath("//select[@id='account']/option[@value='Sarker']")).click();
Random rnd = new Random();
int randomNumber = rnd.nextInt(999);
String description = "Shopping"+randomNumber;
String amount = String.valueOf(randomNumber);
//driver.findElement(By.name("description")).sendKeys(description);
driver.findElement(By.name("amount")).sendKeys(amount);
driver.findElement(By.id("submit")).click();
//Thread.sleep(5000);
By recentDepositLocator = By.linkText(description);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(recentDepositLocator));
boolean status = driver.findElement(recentDepositLocator).isDisplayed();
if(status==true) {
System.out.println("Test Passed");
} else System.out.println("Test Failed!!");
}
Homework
Automate the following test case:
Scenario: Users will be able to add deposit
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 Add Deposit button on Dashboard Page
6. Click on Open An Account drop down to expand it,
7. Click on any account name,
8. Type any description,
9. Type any amount,
10. Click on submit button,
Visually check to make sure the deposit posted
Interview Questions
1. Name various element locators in Selenium.
2. If the element is a link, which locator should you use?
3. Which element locator is faster to identify?
4. Which element locators should be your first priority?
5. What are the benefits of cssSelector?
6. What is the difference between absolute xpath and relative xpath?
7. What is the basic syntax of relative xpath?
8. When should you use contains in xpath?
9. What is the difference between implicit and explicit waits?
10. When and what type of wait is needed in automation?
2. If the element is a link, which locator should you use?
3. Which element locator is faster to identify?
4. Which element locators should be your first priority?
5. What are the benefits of cssSelector?
6. What is the difference between absolute xpath and relative xpath?
7. What is the basic syntax of relative xpath?
8. When should you use contains in xpath?
9. What is the difference between implicit and explicit waits?
10. When and what type of wait is needed in automation?
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.