Sunday, June 26, 2016

Selenium Interview Questions - Very Important


Ques1: How many different element locators available in Selenium WebDriver?
driver.findElement(By.id("HTMLid"));
driver.findElement(By.name("HTMLname"));
driver.findElement(By.cssSelector("cssLocator"));
driver.findElement(By. className ("CalssName”));
driver.findElement(By. linkText ("LinkeText”));
driver.findElement(By. partialLinkText ("PartialLink”));
driver.findElement(By. tagName ("TanName”));
driver.findElement(By.xpath("XPathLocator));

Ques2: How we can submit the form in WebDriver?
WebElement el  =  driver.findElement(By.id("ElementID"));
el.submit();

Ques3: How to execute Java Script Functions?
JavascriptExecutor js = (JavascriptExecutor) driver;
String title = (String) js.executeScript("pass your java scripts");

Ques4: How to automate radio button object in WebDriver?
WebElement el = driver.findElement(By.id("Radio button id"));

//to perform check operation
el.click()

//verfiy to radio button is check it return true if selected else false
el.isSelected()

Ques5: How to verify the text of the object?
WebElement el  =  driver.findElement(By.id("ElementID"));

//get test from element and stored in text variable
String text = el.getText();

//assert text from expected
Assert.assertEquals("WebElement Text", text);

Ques6: How to perform double click on an element?
DoubleClick() method we can use with the use of Actions Class
WebElement el  =  driver.findElement(By.id("ElementID"));
Actions builder = new Actions(driver);
builder.doubleClick(el).build().perform();

Ques7: How to perform drag and drop on an element?
WebElement source  =  driver.findElement(By.id("Source ElementID"));
WebElement destination  =  driver.findElement(By.id("Taget ElementID"));
Actions builder = new Actions(driver);
builder.dragAndDrop(source, destination ).perform();

Ques8: How to work on dropdown in webdriver?
Select sel= new Select(driver.findElement(By.id("drop down ID")));
sel.selectByVisibleText("Pass your desire Text");
 Select drop down by value

Select sel= new Select(driver.findElement(By.id("drop down ID")));
sel. selectByValue( ("Pass your desire Text");
 Select drop down by index

Select sel= new Select(driver.findElement(By.id("drop down ID")));
sel. selectByIndex (0);  // 0 is first index
 Verify dropdown supported multiple options or not

Select sel= new Select(driver.findElement(By.id("drop down ID")));
Boolean status = sel. isMultiple()
Get number of options are available in drop down

Ques9: How to create dynamic xpath?
For example that we have a xpath with changing id(displaying in redcolor)

//div[@id='post-body-3647323225296998740']/div[1]/form[1]/input[1]

 There are many ways to handle this situation we are discussing all ways one by one

1st Way:

Look for any other attribute which is not changing every time in that div node like name,class etc
If this div has class attribute which are not changing then we can write our xpath as:

//div[@class='post-body entry-content']/div[1]/form[1]/input[1]

2nd Way:

We can use absolute path(Full xpath) where you not need to provide any attribute
/html/body/div[3]/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/div/div[4]/div[1]/div/div/div/div[1]/div/div/div/div[1]/div[2]/div[1]/form[1]/input[1]

3rd Way:
We can use starts-with function
//div[starts-with(@id,'post-body-')]/div[1]/form[1]/input[1]

4th Way:
We can use contains function
div[contains(@id,'post-body-')]/div[1]/form[1]/input[1]

Ques10: Waits in WebDriver?
Webdriver has two types of wait
1. Implicit Wait
2. Explicit Wait

Implicit Wait:
Sometimes, Elements are taking time to be appear on page. Using Implicit
wait In webdriver test case, We can poll the DOM for certain amount of time
when some element or elements are not available Immediately on webpage.

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Explicit Wait:(Web driver wait)
Using explicit wait code In selenium webdriver, You can define to
wait for a certain condition to occur before proceeding further test code
execution.
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='gbqfq']")));
Abovecode will wait for 20 seconds for targeted element to be displayed and enabledor we can say clickable.

Ques11: How to handle alert in Webriver?
@Test
 public void Text() throws InterruptedException {
  //Alert Pop up Handling.
  driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();
  //To locate alert.
  Alert A1 = driver.switchTo().alert();
  //To read the text from alert popup.
  String Alert1 = A1.getText();
  System.out.println(Alert1);
  Thread.sleep(2000);
  //To accept/Click Ok on alert popup.
  A1.accept();
 
  //Confirmation Pop up Handling.
  driver.findElement(By.xpath("//button[@onclick='myFunction()']")).click();
  Alert A2 = driver.switchTo().alert();
  String Alert2 = A2.getText();
  System.out.println(Alert2);
  Thread.sleep(2000);
  //To click On cancel button of confirmation box.
  A2.dismiss();
 
  //Prompt Pop up Handling.
  driver.findElement(By.xpath("//button[contains(.,'Show Me Prompt')]")).click();
  Alert A3 = driver.switchTo().alert();
  String Alert3 = A3.getText();
  System.out.println(Alert3);
  //To type text In text box of prompt pop up.
  A3.sendKeys("This Is John");
  Thread.sleep(2000);
  A3.accept(); 
 }
}

Ques12: How to work with properties file or Object Repository in Webdriver?
Create the properties file and read out this file in java using below code

@Test
  public void Calc_Operations() throwsIOException{
  //Create Object of Properties Class.
  Properties obj = new Properties();  
  //Create Object of FileInputStream Class. Pass file path.
  FileInputStream objfile = newFileInputStream(System.getProperty("user.dir")+"\\src\\ObjectRepo\\objects.properties");
  //Pass object reference objfile to load method of Properties object.
  obj.load(objfile);
 
  //Sum operation on calculator.
  //Accessing element locators of all web elements using obj.getProperty(key)
  driver.findElement(By.id(obj.getProperty("eight"))).click();
  driver.findElement(By.id(obj.getProperty("plus"))).click();
  driver.findElement(By.id(obj.getProperty("four"))).click();
  driver.findElement(By.id(obj.getProperty("equalsto"))).click();
  String i = driver.findElement(By.id(obj.getProperty("result"))).getAttribute("value");

  System.out.println(obj.getProperty("eight")+" + "+obj.getProperty("four")+" = "+i);


No comments:

Post a Comment