Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

Wednesday, 5 February 2020

Alert popup handling


We are not able to inspect the alerts and some popups in the browsers. In such cases we have to switch the focus from the screen to the pop up. For switching there is a ‘Switch’ option available in selenium.

Example:

 System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
 WebDriver driver=new ChromeDriver();
      
Alert or popup handling

driver.get("https://mail.rediff.com/cgi-bin/login.cgi");

driver.findElement(By.xpath("//input[@type='submit']")).click();
Alert alertbox=driver.switchTo().alert();
       
String alertMessage=alertbox.getText();
System.out.println(alertMessage);
Assert.assertEquals("Please enter a valid user name", alertMessage);
driver.switchTo().alert().accept();

driver.switchTo().alert().dismiss();
driver.switchTo().alert().getText();
driver.switchTo().alert().sendKeys(keysToSend);

Handle Drop down


We can select a value from the dropdown using the select class.

Example:

Select edition=new Select(driver.findElement(By.id("payment_plan_id")));
edition.selectByVisibleText("Free Edition");


edition.selectByIndex(index);
edition.selectByValue(value);
edition.selectByVisibleText(text);
             
edition.deselectAll();
edition.deselectByIndex(index);
edition.deselectByValue(value);
edition.deselectByVisibleText(text);   

Locators in Selenium


There are eight locators in selenium. We can priorities the locators as below:

1.id

2.cssSelector

3.xpath

4.name

5.className

6.linkText

7.partialLinkText

8.tagName


Xpath are of two types

Absolute XPath

Eg: html/body/div[1]/section/div[1]/div/div/div/div[1]/div/div/div/div/div[3]/div[1]/div/h4[1]/b

Relative XPath

Eg: //*[@class='featured-box']//*[text()='Testing']

Absolute xpath should not be used, only Relative Xpath should be used in the selenium scripting