Blog

Advanced Selenium Interview Questions

Share:

1. What will happen if you mix both implicit wait and explicit wait in a Selenium Script?

According to the official Selenium documents, it is being suggested to not to mix both Implicit and Explicit Waits. If we mix both of them then it can cause unpredictable wait times.
Implicit wait is defined only once in the code and it will remain same throughout the driver object instance.
Explicit wait is defined wherever it is necessary in the code and it  will called at the time of execution. Explicit wait will overwrite the implicit wait if applied in the code. So, Explicit Wait always gets first preference and the then Implicit Wait is executed.

2. What is MaxSession Properties in Selenium Grid?

MaxSession is defined as a number of browsers, independent of the type & version, can run in parallel on the remote machine.

MaxSession overrides the MaxInstances settings and hence can restrict the number of browser instances that can run in parallel.

For eg.

If maxSession=1 then only a single browser will run in remote machine. If maxSession=2 then any two browsers can run at a time irrespective of MaxInstances that we defined. It can be either 2 Internet Explorer or 2 FireFox or 1 Internet Explorer and 1 Firefox.

browser browserName=firefox,maxInstances=5,maxSession=2

3. What will happen if you run this command. driver.get(“www.devlabsalliance.com”) ;?

Since the given URL doesn’t contain http or https prefix hence it will throw an exception.

So, it is required to pass HTTP or HTTPS protocol within driver.get() method.

driver.get(“https://www.devlabsalliance.com”);

4. What is MaxInstances Properties in Selenium Grid?

MaxInstances is defined as the number of browser instances of the same version of the browser that can run on the remote machine.

for eg.

-browser browserName=InternetExplorer,version=11,maxInstances=3,platform=WINDOWS

-browser browserName=Firefox,version=12,maxInstances=2,platform=WINDOWS

This will allow to run 5 instances (3 instances of IE and 2 instances of Firefox) at the same time in a remote machine.

5. What is MaxSession Properties in Selenium Grid?

MaxSession is defined as a number of browsers, independent of the type & version, can run in parallel on the remote machine.

MaxSession overrides the MaxInstances settings and hence can restrict the number of browser instances that can run in parallel.

For eg.

If maxSession=1 then only a single browser will run in remote machine. If maxSession=2 then any two browsers can run at a time irrespective of MaxInstances that we defined. It can be either 2 Internet Explorer or 2 FireFox or 1 Internet Explorer and 1 Firefox.

browser browserName=firefox,maxInstances=5,maxSession=2

6. How can we handle Web-based Pop-ups or Alerts in Selenium?

Web-based alerts or popups are handled by switching to the alert window and calling Selenium WebDriver Alert API methods.
Below methods are called for various actions:

  • dismiss(): To click on Cancel button.
  • accept(): To Click on OK button.
  • getText(): To get the text which is present on the Alert.
  • sendKeys(): To enter the text into the alert box

7. What happen if you mix both Thread.Sleep and WebDriver Waits in a Selenium Script?

Thread.sleep() method is used to pause the execution of script for specified time in milliseconds

If WebDriver waits are used along with Thread.sleep() method, then webdriver will hold the execution for specified time (as mentioned in Thread.sleep()) and then will follow other wait. If we mix both the waits, then test execution time will increase.

8. How can you use the Recovery Scenario in Selenium WebDriver?

Recovery Scenarios are used by using “Try Catch” block within Selenium WebDriver Java tests.

try{
driver.get(“https://www.devlabsalliance.com“);
}
catch{
System.Out.println(e.getMessage());
}

9. What is Robot Class in Selenium and what are its advantages?

Robot Class is used to control the keyboard or mouse to interact with Operating System windows like download pop-ups, alerts, print pop-ups, etc or native OS applications like Notepad, Calculator, Skype, etc. while doing browser automation.

The various advantages of Robot Class are:

  • It simulates Mouse and Keyboard events.
  • It helps in upload/download of files while using Selenium webdriver.
  • It gets easily integrated with current automation framework (keyword-driven, data-driven or hybrid framework).

10. What are various Robot Class internal methods and explain their usage.

Some of the Robot Class Internal methods and their usage are as follows:

  • keyPress(): eg. : robot.keyPress(KeyEvent.VK_Down) : This method will press down the arrow key of keyboard.
  • mouseMove(): eg. robot.mouseMove(point.getX(), point.getY()) : This will move the mouse pointer to specific X and Y coordinates.
  • mousePress(): eg. robot.mousePress(InputEvent.BUTTON_DOWN_MASK): This method will press the right click of mouse.
  • mouseRelease(): eg. robot.mouseRelease(InputEvent.BUTTON_DOWN_MASK): This method will release the right click of mouse.
  • keyRelease(): eg. robot.keyRelease(KeyEvent.VK_DOWN): This method will release the down arrow key of keyboard.

11. How to Upload a file in Selenium WebDriver?

We can upload a file in Selenium WebDriver using the AutoIT script. Follow the following steps for uploading a file with AutoIT script:

  • Download and Install the “AutoIT”.
  • Open Eclipse and write code through Selenium WebDriver for clicking on the “Upload File” button.
  • Ensure FileUpload.exe is generated after compilation in the location where AutoIT is installed. This FileUpload.exe already contains the file(say, devLabsAlliance.docx) to be uploaded and this is pre-configured through AutoIT editor.
  • After click of Upload button, AutoIT script needs to be called and then control immediately transferred to AutoIT in order to upload a file and then control is sent back to Selenium.
    Syntax for calling AutoIT script:
    Runtime.getRuntime().exec(“D:\\AutoIT\\FileUpload.exe”);

12. How to run Selenium test from Command Line in Selenium WebDriver?

For doing this, we need to install TestNG, and need to set the class-path.

Follow the below steps:

  • Create testng.xml file and store this xml file into project home directory.
  • Ensure that you put all the Selenium jars in separate folder and keep that folder into project home directory.
  • Open Command Prompt and type cd\ and then press Enter.
  • Now copy the path of your project home directory in this cd directory path and then press Enter.
  • Now set the classpath for this and specify the bin folder.
    Project Directory > set classpath=Project Directory\bin; and press enter
  • Now specify the folder(say, lib) where all the jars are available.
    Project Directory > set classpath=Project Directory\lib\*; and press enter
  • Now run xml file using below command:
    Project-directory > java org.testng.TestNG testng.xml and hit Enter.

13. How to connect a database in Selenium?

To connect a database in Selenium, we use JDBC driver while using Java Programming language.

Follow the below steps to connect database in Selenium:

  • Load the required JDBC driver class.
    Class.forName(“net.sourceforge.jtds.jdbc.Driver”);
  • Establish a connection to the database:
    Connection con = DriverManager.getConnection(“DataBaseURL”, “userName”, “password”);
  • Connection URL:
    jdbc:sqlserver://ipAddress:portNumber/dbName
  • Execute SQL Queries:
    Statement sqlStatement = con.createStatement();
    String sqlQuery = “SELECT * FROM table_name WHERE condition”
    ResultSet resSet = sqlStatement.executeQuery(sqlQuery);
  • Fetching data from resultSet:
    while (resSet.next()) {
    System.out.println(resSet.getString(required_column_name));}
  • Closing the database connection. con.close();

14. How to Scroll Web Page Down Or Up Using Selenium WebDriver?

To scroll the page Up or Down, JavaScript scrollBy() method is used.

Syntax for Scrolling WebPage down:
WebDriver driver = new ChromeDriver();driver.navigate().to(“https://www.devlabsalliance.com”);JavascriptExecutor js = (JavascriptExecutor) driver;js.executeScript(“window.scrollBy(0, 250)”, “”);Syntax for Scrolling WebPage Up:
WebDriver driver = new ChromeDriver();driver.navigate().to(“https://www.devlabsalliance.com”);JavascriptExecutor js = (JavascriptExecutor) driver;js.executeScript(“window.scrollBy(0, -250)”, “”);

15. What is Desired Capabilities?

Desired capabilities are used in to handle SSL certificates in Chrome browser.

To achieve this, we need to create an instance of DesiredCapabilities:

DesiredCapabilities desiredCapability = DesiredCapabilities.chrome();

16. How to Highlight Element Using Selenium WebDriver?

We can highlight the specified element using JavaScriptExecuter interface.

WebDriver driver = new ChromeDriver();

driver.get(“https://www.devlabsalliance.com”);

WebElement ele = driver.findElement(By.xpath(“//*[@id=’devLabs’]”));

JavascriptExecutor js = (JavascriptExecutor) driver;

//Passing values based on css style. Yellow background color with solid red color border.

js.executeScript(“arguments[0].setAttribute(‘style’, ‘background: yellow; border: 3px solid red;’);”, ele)

17. How to launch a batch file in a Selenium webdriver?

Before starting the automation, we need to set up the pre-requisites in a test suite to run a batch file or an executable file.

Below code can be used for the same:

Process batchFile = Runtime.getRuntime.exec(“Path of batch file”);

batchFile.waitFor();

18. How Selenium Webdriver test can be run from the command line?

Below code is used to write Selenium Webdriver test using command line:

java -classpath “.;selenium-server-standalone-2.33.0.jar” ExampleClass

19. Which Selenium technology is useful for distributed data processing?

Selenium Grid is popularly used for distributed data processing. Selenium Grid distributes tests on multiple machines in parallel.

The tests can be executed in parallel on different operating systems and different web browsers at the same time with a single script using Selenium Grid.

Distributed data processing in Selenium reduces overall time of execution and feedback is also quick in distributed data process.

Explain what the following snippet of Java code does in brief:
WebElement example = driver.findElement(By.xpath(“//*[contains(text(), ‘data’)]”));

The above code is defining a variable ‘example’ of type WebElement and it is initializing it with a reference to an element that contains the text value “data” using XPath search.

20. What is StaleElementException?

StaleElementException is an exception which is thrown when the element that is invoked is no longer attached to the DOM(Document Object Model) for any reason.

For eg. If the element found in a web page is referenced as a WebElement in the Webdriver. Now if the DOM changes, then WebElement is no longer available, hence WebElement goes stale. Then if we try to interact with this stale element, then the StaleElementException is thrown.

Related Articles
Categories
Latest Articles

Popular Courses

Google Reviews
  • review rating 5  Training provided by the trainers here is completely awesome. Trainer is highly qualified in their areas with practical experience. Also amenities provided during training by institute is great

    thumb Abhishek Tripathi
    6/02/2019
  • review rating 5  Excellent Training program. Training courses and trainers are very good. Trainer will explain you as many time you ask the question. Best thing is hands on based training. You learn by doing. Looking for more courses from here.

    thumb Vijay Kumar
    6/30/2019
  • review rating 4  Both trainers (Ravi and Naresh) were excellent in their approach. Session was less of a theory and more of a practical. And even the theory part was quite interesting. A bit issue with the volume of information which I had received in just a span of 2-3 days - it seemed a bit over-loaded. But overall, it has given me a great platform to take it forward from here!

    thumb Varenya Varshney
    6/02/2019
  • review rating 4  Training provided by the trainers in DevLabs Alliance is completely awesome. Trainers are highly qualified in their areas with practical experience. Since it is 3 days training we have completely got the opportunity to have hands on.

    thumb Salaja K
    6/30/2019
  • review rating 4  It was good session, I left my training room with basic knowledge on all the tools. Tutors have great knowledge and experience on the tools. But three days is not enough time.

    thumb sairam shyamala
    6/30/2019
$('.enrollbtn').on('click', function (){ $( "#announcement" ).tabs( "option", "active", 0 ); $(' a[href="#announcement"]').trigger('click'); });