Selenium Interview Questions
1. Create a TestNG test which opens a web page with button(id=alert) clicking on which alert window opens up. Use Page Factory Implementation
Ans:
private By Alert =By.id("alert"); //Page Object Model
@FindBy(id="alert") private WebElement alert; // Page factory Model1
@FindBy(how =How.ID,using ="alert") private WebElement alert1; //Page factory Model2
@Test
public void test3() throws InterruptedException
{
//Code to open a page
driver.findElement(Alert).click() //Using Page Object Pattern
PageFactory.initElements(driver,this); //Using Page Factory Model1
alert.click(); //Using Page Factory Model2
alert1.click()
}
2. What is CacheLookup annotation in PageFactory?
Ans:
annotation @CacheLookup to WebElements to indicate that it never changes (that is, that the same instance in the DOM will always be used)
CacheLookup attribute can be used to instruct the InitElements() method to cache the element once its located and so that it will not be searched over and over again – this is useful when the elements that are always going to be there
(For AJAX based applications, it may not work where the DOM changes based on user action on the page). Otherwise every time when we use a Web Element the WebDriver will go and search it again
Usage
@FindBy(id="alert")
@CacheLookup
private WebElement Alert;
3. How to download webdriver executables automatically?
Ans:
Add Below dependency in the Maven
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
Note: Webdriver manager internally uses commons-io. so if you are using this, then comment it
<!-- <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>-->
Usage:
private WebDriver driver;
@BeforeClass
public void setUp() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
4. Read all values from properties file
Ans:
File propfile = new File("propfile.properties");
FileInputStream fis = new FileInputStream(propfile);
Properties prop = new Properties();
prop.load(fis);
System.out.println(prop.get("url")); //accessing single key value
//Accessing all key values
Enumeration keyvalues = prop.keys();
while(keyvalues.hasMoreElements())
{
String key=(String)keyvalues.nextElement();
String value = prop.getProperty(key);
System.out.println("Key:="+key + "Value:="+value);
}
5. Difference between Exception and Error?Example for each
Ans:
Error:
public class HelloWorld{
public static void main(String []args){
test(5);
}
public static void test(int i)
{
if (i == 0)
return;
else {
test(i++);
}
}
}
Output:
Exception in thread "main" java.lang.StackOverflowError
at HelloWorld.test(HelloWorld.java:14)
at HelloWorld.test(HelloWorld.java:14)
at HelloWorld.test(HelloWorld.java:14)
Exception:
class xception{
// main driver method
public static void main(String[] args)
{
int a = 5, b = 0;
// Try-catch block to check and handle exceptions
try {
// Attempting to divide by zero
int c = a / b;
}
catch (ArithmeticException e) {
// Displaying line number where exception occured
// using printStackTrace() method
e.printStackTrace();
}
}
}
Output:
java.lang.ArithemeticException: /Zero at Xception.main
6. Selenium Architecture
example1:
driver.get("https://username:password@dreamshop.honda.com/s")
example2:
String URL = "https://" +username +":" +password +"@"+ "the-internet.herokuapp.com/basic_auth";driver.get(URL);
Alert alert = driver.switchTo().alert();
alert.accept();
8. What is the difference between ChromeOptions and DesiredCapabilities?
ChromeOptions is a class that can be used to manipulate capabilities specific to ChromeDriver.
For instance, you can disable Chrome extensions with:
ChromeOptions options = new ChromeOptions()
options.addArgument(“disable-extensions”);
ChromeDriver driver = new ChromeDriver(options);
DesiredCapabilities can also be used to manipulate a ChromeDriver session.
To change individual web driver properties, DesiredCapabilities class provides key-value pairs.
But, ChromeOptions supports limited clients whereas DesiredCapabilities supports a vast number of clients.
ChromeOptions is supported by Java and other languages.
DesiredCapabilities is available in Java,
but its use is deprecated.
When working with a client library, like Selenium Ruby Client,
the ChromeOption class will not be available and DesiredCapabilities will have to be used.
9. Opening Browser in Incognitor window
Chrome:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("incognito");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
FireFox:
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.privatebrowsing.autostart", true);
Internet Explorer:
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, true);
capabilities.setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
Opera:
DesiredCapabilities capabilities = DesiredCapabilities.operaBlink();
OperaOptions options = new OperaOptions();
options.addArguments("private");
capabilities.setCapability(OperaOptions.CAPABILITY, options);
Comments