August 21, 2020 • ☕️ 6 min read
Previous posts of frequently asked Selenium interview questions and answers for freshers are Q#1-Q#10, Q#21-Q#30, Q#31-Q#40, Q#41-Q#50
Host: It is the parameter which we use to bind Selenium to a specific IP. Usually, we run selenium tests on our local machine so the value will be ‘localhost’. You can sepcify IP address instead of localhost.
java -jar <selenium server standalone jar name> -host <Your IP Address>
Port Number: TCP/IP port which is used to connect selenium tests to the selenium grid hub. Default port hub is 4444.
java -jar
Make sure no other application in your system is using this port. You may face an exception like Exception in thread “main” java.net.BindException: Selenium is already running on port 4444. Or some other service is.
If this occurs you can either shutdown the other process that is using port 4444, or you can tell Selenium-Grid to use a different port for its hub. Use the -port option for changing the port used by the hub.
java -jar <selenium server standalone jar name> -role hub -port 4441
Browser: To pass the browser which has to execute our selenium scripts
URL: To pass the application URL
Single Slash “/” – Single slash is used to create XPath with absolute path i.e. the XPath would be created to start selection from the document node/start node.
Double Slash “//” – Double slash is used to create XPath with relative path i.e. the XPath would be created to start selection from anywhere within the document.
Soft Assert: Soft Assertions are customized error handlers provided by TestNG. Soft Assertions do not throw exceptions when assertion fails, and they simply continue to the next test step. They are commonly used when we want to perform multiple assertions.
Example:
@Test
public void softAssert() {
SoftAssert softAssertion = new SoftAssert();
System.out.println("Soft Assertion started");
softAssertion.assertTrue(false);
System.out.println("soft assertion executed");
}
Hard Assert: Hard Assert throws an AssertException immediately when an assert statement fails and test suite continues with next @Test
Example:
@Test
public void sostAssert() {
SoftAssert softAssertion = new SoftAssert();
System.out.println("Soft Assertion started");
softAssertion.assertTrue(false);
System.out.println("soft assertion executed");
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
}
@Test
public void hardAssert() {
System.out.println("Hard asserion started");
org.testng.Assert.assertTrue(false);
System.out.println("Hard asser executed");
}
Using WebDriverManager, we can automatically download the driver’s binary files (.exe files) for Web Automation.
What is WebDriverManager in Selenium?
How to set driver binary automatically using WebDriverManager?
First you need to add the dependency in pom file as below:-
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
Code example
public void WebDriverManagerTest()
{
//setup the chromedriver using WebDriverManager
WebDriverManager.chromedriver().setup();
//Create driver object for Chrome
WebDriver driver = new ChromeDriver();
//Navigate to a URL
driver.get("https://toolsqa.com");
//quit the browser
driver.quit();
}
FirefoxDriver is a Java class, and it implements the WebDriver interface.
driver.close(): This command closes the browser’s current window. If multiple windows are open, the current window of focus will be closed.
driver.quit(): When quit() is called on the driver instance and there are one or more browser windows open, it closes all the open browser windows.
Syntax:
executeScript("window.scrollBy(x-pixels,y-pixels)");
First, create a JavaScript object
JavascriptExecutor js = (JavascriptExecutor) driver;
Launch the desired application
driver.get(“https://www.nishant.in”);
Scroll down to the desired location
js.executeScript("window.scrollBy(0,1000)");
The window is not scrolled vertically by 1000 pixels
General Syntax:
driver.findElement(By.id(“id“)).getCssValue(“name of css attribute”);
Example:
driver.findElement(By.id(“email“)).getCssValue(“font-size”);