Selenium Interview Questions and Answers

>   How would do proxy setting using Webdriver.

                FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", 3128);
WebDriver driver = new FirefoxDriver(profile); 


Ho to work with Https websites or / how to handle SSL certificate ?

FirefoxProfile profile = new FirefoxProfile();
profile.setAssumeUntrustedCertificateIssuer(false);
driver = new FirefoxDriver(profile);
driver.get("https://google.com/ <https://mailbox.com/> ");


>   Where would you interface and abstract class

 Our framework is entirly based on keyworddriven framework , so we have never used Interface & abstract class ,

ans : we can use interface for to keep some constant data like (Appurl , psw , username , server path ,result path etc)


>   If you have 10 test scripts. I want to fail 4 of them, how will it be done.
  we can fail the test case 2 ways
1. fail the junit test case through Asserstion
2. fail the test case using custom methode (eg SetExcelResult methode used for result )


>   How would write same script for two 'urls'
I will send u very shortly
Where do you store your scripts
Yes we have used one centralized shared repository  (Hudson tool ) to store all our test scripts,  before starting the work, we used to import all the file from  repository to our local machine and start writing & modified in our local maching , and end of the day upload all the changes to CI tool (Hudson)

>   Where do you run all your scripts - in local machine or server or somewhere else
We do scripts in our local machine , one scripting part is done , we run batch execution in server machine (Vm machine , that machine dedicated to automation execution).
How do you run your scripts in multiple machine
We can run using selenium grid
>   How would you handle dynamic id (id keeps changing and there is nothing no class name , partial link or anything)
Ans:   3 ways we can handle
1: using following::sibling or contains(test(), ‘constantId’)  xpath function
2: using findelements method in webdriver
3: using Table concepts

>   Version of Webdriver
2.25 (latest version)
>   Using java code how will you upload a file

Ans 1 :(using selenium code) driver.findElement(By.id("inputFile")).sendKeys("C:/path/to/file.jpg");

Ans 2 :(using java Code)

driver.findElement(By.id("SWFUpload_0")).click();
Robot r = new Robot();
r.keyPress(KeyEvent.VK_C);        // C
r.keyRelease(KeyEvent.VK_C);
r.keyPress(KeyEvent.VK_COLON);    // : (colon)
r.keyRelease(KeyEvent.VK_COLON);
r.keyPress(KeyEvent.VK_SLASH);    // / (slash)
r.keyRelease(KeyEvent.VK_SLASH);
// etc. for the whole file path

r.keyPress(KeyEvent.VK_ENTER);    // confirm by pressing Enter in the end
r.keyRelease(KeyEvent.VK_ENTER);


>   How would you retrieve xpath from object repository
Using POI library  we have written custom method (Get excel data)to get xpath from excel file

page object factory selenium   : please type this text in google , and have knowledge on this
Page object model its just a framework design pattern preferred by google

this is another way of designing a framework(even net app also design their framework using Page object model concept)




// how to get the number of alert in a page
public class SeleniumRCFirstCode {
private static Selenium selenium;
public static WebDriver driver;
public static void main(String[] args) {
driver = new FirefoxDriver();
int alertcount = getAlert();
}

static int getAlert() {
boolean flag = true;
int alertCount = 0;
while (flag) {

try {
Alert alert = driver.switchTo().alert();
alertCount = alertCount + 1;
alert.accept();
} catch (NoAlertPresentException e) {
System.out.println("all the alert are closed");
System.out
.println("Number alert prasent in UI : " + alertCount);
flag = false;
}
}

return alertCount;
}

}


How to work with Safari browser :
WebDriver doesn’t support as many browsers as Selenium RC does, so in order to provide that support while still using the WebDriver API, you can make use of the SeleneseCommandExecutor
Safari is supported in this way with the following code (be sure to disable pop-up blocking):

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("safari");
CommandExecutor executor = new SeleneseCommandExecutor(new URL("http://localhost:4444/"), new URL("http://www.google.com/"), capabilities);
WebDriver driver = new RemoteWebDriver(executor, capabilities);


How to Run JAVA SCRIpt using webdriver:

WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("write ur java script here ");


Why is it not possible to interact with hidden elements?

Since a user cannot read text in a hidden element, WebDriver will not allow access to it as well.
However, it is possible to use Javascript execution abilities to call getText directly from the element:
WebElement element =((JavascriptExecutor) driver).executeScript("return arguments[0].getText();", element);


How to add firebug to mozilla firefox through webdriver Code

File file = new File("firebug-1.8.1.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1"); // Avoid startup screen
WebDriver driver = new FirefoxDriver(firefoxProfile);

Data Base Code

import java.sql.*;
public class DatabaseFetch{
public static void main(String[] args) throws Throwable {

//Resgister the driver through
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("registered driver successfully");
//Create the connection and assign to connection reference
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "username", "password");
System.out.println("connection successsfully");
//create a statement through connection reference and assign to statement reference
Statement stmt=con.createStatement();
System.out.println("statement object created successfully");
//call the executequery method through statement reference and pass the query as argument.
ResultSet rs=stmt.executeQuery("select * from emp");

System.out.println("query is executed");

while(rs.next()){
int i=rs.getInt(1);
String str=rs.getString(2);
String str1=rs.getString(3);
int i1=rs.getInt(4);
System.out.println(i+"\t"+str+"\t"+str1+"\t"+i1);

}

}

}

Copyright © 2017 qatoolsguide.blogspot.com || ALL RIGHTS RESERVED