Sunday, November 17, 2013

Selenium Web driver interview questions & answers(very important)


Please find the below important question & answers which will ask in interview for Webdriver

Selenium WebDriver methods with Examples

1.Browser Back and Forward (NAVIGATION)

Steps to implement Browser back and forward through Selenium Web Driver

1. Create Driver for any Browser(Mozilla)

2. Go to the URL

3. Navigate to some page in website.

4. Use Selenium code to Navigate Back to Main Page.

CODE: driver.navigate().back();

 driver.navigate().forward();

Example

WebDriver driver =new FirefoxDriver();

driver.get("http://seleniumhq.org/");

driver.findElement(By.linkText("Download")).click();

Thread.sleep(3000); //delay

driver.navigate().back();

driver.navigate().forward();

------------------------------------------------------------------------------------------------------------------------------


2.Handling DRAG and DROP



Steps to Handle Drag and Drop through Selenium Web Driver



1. Create Driver for any Browser(Mozilla)

2. Go to the URL

3. Create an Action object for Driver

4. Fetch and create WebElement object for the SOURCE element.

5. Fetch and create WebElement object for the DESTINATION element.

6.Perform ACTION

 1.Click and Hold the source WebElement

 2.Move to destination WebElement

 3.Release the Element.

Example


WebDriver driver = new FirefoxDriver();
driver.get("http://www.ericbieller.com/examples/dragdrop/");
driver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
Actions act = new Actions(driver);
WebElement src = driver.findElement(By.xpath("//div[@id='items']/div[1]"));
WebElement des = driver.findElement(By.id("trash"));
act.clickAndHold(src).build().perform(); //For each action we need to build
and Perform
act.moveToElement(des).build().perform();
act.release(des).build().perform();----------------------------------------------------------------------------------
--------------------------------------------

3. How to get number of links in a Web page.(Google page)

 
06 November 2013 19:09:36
package practice_webdriver;
02.import java.util.List;
03.   
04.import java.util.concurrent.TimeUnit;
05.import org.openqa.selenium.*;
06.import org.openqa.selenium.firefox.FirefoxDriver;
07.import org.openqa.selenium.support.ui.ExpectedConditions;
08.import org.openqa.selenium.support.ui.WebDriverWait;
09.   
10.public class AllLinks {
11.   
12.    public static void main(String[] args) {
13.        String baseUrl = "http://newtours.demoaut.com/";
14.        WebDriver driver = new FirefoxDriver();
15.        String underConsTitle = "Under Construction: Mercury Tours";
16.        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
17.   
18.        driver.get(baseUrl);
19.        List<WebElementlinkElements = driver.findElements(By.tagName("a"));
20.        String[] linkTexts = new String[linkElements.size()];
21.        int i = 0;
22.   
23.        //extract the link texts of each link element
24.        for (WebElement e : linkElements) {
25.            linkTexts[i] = e.getText();
26.            i++;
27.        }
28.   
29.        //test each link
30.        for (String t : linkTexts) {
31.            driver.findElement(By.linkText(t)).click();
32.            if (driver.getTitle().equals(underConsTitle)) {
33.                System.out.println("\"" + t + "\""
34.                        + " is under construction.");
35.            } else {
36.                System.out.println("\"" + t + "\""
37.                        + " is working.");
38.            }
39.            driver.navigate().back();
40.        }
41.        driver.quit();
42.    }

No comments:

Post a Comment

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...