Selenium Java Interview Questions and Answers Part-23
1) What is a headless browser?
Headless browsers provide automated control of a web page in an environment similar to popular web browsers, but they are executed via a command-line interface. Some of the headless browsers are Chrome Headless, PhantomJS, HTMLUnit etc…
2) How frequently do you use Thread.Sleep()?
Thread.Sleep() shouldn’t be used frequently in scripts as it will slow down the execution speed. It stops the execution of the execution of the script for the specified time
3) How does Selenium interact with the Web browser?
When we execute any script in Selenium, every statement is converted as a URL with the help of JSON Wire Protocol over HTTP. The URL is passed over to the browser driver and then that request is passed over to the real browser over HTTP. Finally the commands in the scripts will be executed in the browser.
4) How to click a button without using click() and without using CSS & XPath selectors?
We can click on a button using the JavaScript Executor.
JavaScriptExecutor js = (JavaScriptExecutor)driver; js.executeScript("arguments[0].click()",button);
5) How do you check whether the field is editable or not in selenium?
WebElement element = driver.findElement(By.id("field")); String val = element.getAttribute("readonly"); Assert.assertNotNull(val);
6) How webdriver works?
When a test script is executed with the help of WebDriver, the following tasks are performed in the background:
– An HTTP request is generated and it is delivered to the browser driver for every Selenium Command.
– The HTTP request is received by the driver through an HTTP server.
– All the steps/instructions to be executed on the browser is decided by an HTTP server.
– The HTTP server then receives the execution status and in turn sends it back to the automation scripts.
7) What is By class?
By class is a mechanism used to locate elements within a document. We can create our own locating mechanisms by overriding the protected methods. We can use different nested classes like By.Name, By.Id, By.TagName, etc…
8) Why do we use // for writing xpath?
// represents relative xpath. It can search elements anywhere on the webpage.
9) Write Xpath by using contains?
WebElement element = driver.findElement(By.xpath("//*[contains(@class,'alert')]")); System.out.println(element.getText());
10) What is the difference between thread.Sleep() and selenium.SetSpeed (“2000”)?
setSpeed : Set execution speed (i.e., set the millisecond length of a delay which will follow each selenium operation)
Thread.sleep : It causes the current thread to suspend execution for a specified period.
11) In what situation selenium finding element get fails?
Selenium find element can fail in many situations:
– Element is not present on the page
– Element is not visible on the page
– Element locator has changed
– Operation timed out
12) How we can retrieve the dynamically changing Ids?
We can use different XPATH methods to identify dynamic elements in the webpage.
driver.findElement(By.xpath("//*[starts-with(@id,'qascript')]"));
13) How to refresh a page without using context click?
driver.navigate().refresh();
14) How to get the name of browser using Web Driver?
Capabilities cap = ((RemoteWebDriver) driver).getCapabilities(); String browserName = cap.getBrowserName();
15) How to disable cookies in browser.?
Map prefs = new HashMap(); prefs.put("profile.default_content_settings.cookies", 2); ChromeOptions options = new ChromeOptions(); options.setExperimentalOptions("prefs", prefs); driver = new ChromeDriver(options);
16) How to work with radio button in web driver?
We can click on the radio button in webdriver.
driver.findElement(By.id("rb")).click();
17) Explain Assert.assertEquals()?
We can use assertEquals method to compare 2 objects.
18) What are the most common pre define functions of xpath?
Some of the common pre-defined functions of xpath are starts-with, text() and contains.
19) How to get the snapshot using selenium?
File scrFile = ((TakeScreenshot)driver).getScreenshotAs(ouputType.FILE); File DestFile=new File(filePath); FileUtils.copyFile(SrcFile, DestFile);
20) How to verify the text inside the text box is present or not?
String text = driver.findElement(By.id("username")).getAttribute("value"); Assert.assertTrue(text!=null);
21) What are ancestors & siblings tell me the syntax?
ancestors syntax:
driver.findElement(By.xpath("//a[contains(text(),'Sign')]//ancestor::div[2]"));
siblings syntax:
driver.findElement(By.xpath("//p[text()='Already have account ? ']//following-sibling::a")).click();
22) How will you open the URL using IE Browser in Selenium?
System.setProperty("webdriver.ie.driver", "driverPath"); WebDriver driver=new InternetExplorerDriver(); driver.get("https://www.google.com/");
23) How to switch from frame to main window? With syntax.
driver.switchTo().defaultContent();
24) What is the difference between “type” and “typeAndWait” command?
Type: When the user needs to enter the text into a text field, type command is used.
TypeAndWait: This command is generally used to reload the web page as soon as the typing of the text is completed.
25) How to invoke an application in webdriver?
Runtime.getRuntime().exec("notepad.exe");
Next Steps:
> More interview questions and answers on Selenium Java, continue to the next post (Click on Next Post link below)
> Check complete Selenium Java interview questions and answers here (Click here)
Please leave your questions/comments/feedback below.
Happy Learning ?
Connect to me on Linked In (Click here)
On a mission to contribute to the Software Testing Community in all possible ways.