Selenium interview questions

How Selenium WebDriver Works

On a high-level, Selenium WebDriver works in three steps:

  • Test commands are converted into an HTTP request by the JSON wire protocol.
  • Before executing any test cases, every browser has its own driver, which initializes the server.
  • The browser then starts receiving the request through its driver.


Appium Desired Capabilities

Desired Capabilities are keys and values encoded in a JSON object, sent by Appium clients to the server when a new automation session is requested. They tell the Appium drivers all kinds of important things about how you want your test to work.  


Q1.  How to handle Multiple windows.

Ans:- through getwindowhandles();

driver.get("http://www.naukri.com/");

// It will return the parent window name as a String

String parent=driver.getWindowHandle();

Set<String> s=driver.getWindowHandles();

// Now iterate using Iterator

Iterator<String> I1= s.iterator();

while(I1.hasNext())

{  String child_window=I1.next();

if(!parent.equals(child_window))

{

driver.switchTo().window(child_window);

System.out.println(driver.switchTo().window(child_window).getTitle());

driver.close();

}

}

//switch to the parent window

driver.switchTo().window(parent);


Q2:- 

What is SSL Certificate?

SSL (Secure Sockets Layer) is a standard security protocol for establishing a secure connection between the server and the client which is a browser.


How to handle SSL Certificate Error using Selenium Webdriver

Suppose we have written some test scripts and while executing the script, we caught in the situation as “Untrusted Connection” above then how do we handle the exception purely through automation.

In such case, we have to adjust our script in such a way that it will take care of SSL Exception by itself.

The scripts need to be modified according to the type of browser instance we are using. 

These when desired capabilities comes in picture.

Desired Capabilities is used to configure the driver instance of Selenium Webdriver. Through Desired Capabilities, one can configure all driver instance like ChromeDriver, FirefoxDriver, and Internet Explorer.

As of now we don’t have any specific URL to create the above scenario, but I am providing steps that we can add in the Selenium Script to handle the above situation “Untrusted Connection.”

for chrome browser:-

DesiredCapabilities handlSSLErr = DesiredCapabilities.chrome ()       
handlSSLErr.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true)
WebDriver driver = new ChromeDriver (handlSSLErr);
Q3.

What is Ajax?

AJAX stands for Asynchronous JavaScript & XML, and it allows the Web page to retrieve small amounts of data from the server without reloading the entire page.

Ajax is a technique used for creating fast and dynamic web pages. This technique is asynchronous and uses a combination of Javascript and XML .


How to handle Ajax call Using Selenium Webdriver

The biggest challenge in handling Ajax call is knowing the loading time for the web page. Since the loading of the web page will last only for a fraction of seconds, it is difficult for the tester to test such application through automation tool. For that, Selenium Webdriver has to use the wait method on this Ajax Call.

  1. Thread.Sleep()  
  2. Implicit Wait():-driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
  3. Explicit Wait():- WebDriverWait wait=new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath( "/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i")));
  4. Fluent Wait()
Q4.

How to check the Broken Links and images

For checking the broken links, you will need to do the following steps.

  1. Collect all the links in the web page based on <a> tag.
  2. Send HTTP request for the link and read HTTP response code.
  3. Find out whether the link is valid or broken based on HTTP response code.
  4. Repeat this for all the links captured.

public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        String homePage = "https://www.blogger.com/blog";
        String url = "";
        HttpURLConnection huc = null;
        int respCode = 200;
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get(homePage);
        List<WebElement> links =driver.findElements(By.tagName("a"));
        Iterator<WebElement> it = links.iterator();
        while(it.hasNext()){
            url = it.next().getAttribute("href");
            System.out.println(url);
            if(url == null || url.isEmpty()){
System.out.println("URL is either not configured for anchor tag or it is empty");
                continue;
            }
            try {
       huc = (HttpURLConnection)(new URL(url).openConnection());  
       huc.setRequestMethod("HEAD"); 
       huc.connect();
      respCode = huc.getResponseCode();
       if(respCode >= 400){                    System.out.println(url+" is a broken link");}
 else{  System.out.println(url+" is a valid link");     
     } catch (MalformedURLException e) {              e.printStackTrace();}
 catch (IOException e) { e.printStackTrace();}} driver.quit();}}
Q5.What is a Property file in Java

.properties files are mainly used in Java programs to maintain project configuration data, database config or project settings, etc. Each parameter in properties file is stored as a pair of strings, in key-value pair format, where each key is on one line. You can easily read properties from some file using an object of type Properties. This is a utility provided by Java itself. 

java.util.Properties;

How to load/read property file :-

BufferedReader reader = new BufferedReader(new FileReader(propertyFilePath));
Properties properties = new Properties();
properties.load(reader);

Selenium 3 Architecture

Selenium 3 architecture supports JSON Wire Protocol, here JSON stands for JavaScript Object Notation. Selenium 4 does not include the JSON Wire Protocol and that’s the main difference between selenium 4 and Selenium 3.

JSON Wire Protocol transfers the information from the client to the server over HTTP, here HTTP stands for Hypertext Transfer Protocol, in this, a selenium request is sent from a selenium client, the request is received by the JSON Wire Protocol over HTTP, and secured by the browser Driver, after that a response returned by the server and received by the client.


Selenium 4:- it used WebDriver W3C protocol in order to establish the communication between the Client and Server.

 How to set browser window size in Selenium?

The window size can be maximized, set or resized 

To maximize the window

   driver.manage().window().maximize();

To set the window size

  Dimension d = new Dimension(400,600);

  driver.manage().window().setSize(d);

Alternatively,

The window size can be reset using JavaScriptExecutor

 ((JavascriptExecutor)driver).executeScript("window.resizeTo(1024, 768)");


Q

To handle authentication pop-ups, verify its appearance and then handle them using an explicit wait command.

Use the explicit wait command

  WebDriverWait wait = new WebDriverWait(driver, 10);

Alert class is used to verify the alert

   Alert alert = wait.until(ExpectedConditions.alertIsPresent());

Once verified, provide the credentials

  alert.authenticateUsing(new UserAndPassword)

What is the difference between "type" and "typeAndWait" command?

The "type" command is used to enter text into a field on a web page. The "typeAndWait" command also enters text into a field, but it waits for the page to load before proceeding to the next command. This can be useful if you are unsure whether or not the text you entered will cause the page to refresh.

The main difference between the "type" and "typeAndWait" command is that the "type" command does not wait for the page to reload, while the "typeAndWait" command does. If you are unsure whether or not the text you are entering will cause the page to refresh, it is best to use the "typeAndWait" command. This way, you can be sure that the next command will not be executed until the page has finished loading.


How can we launch different browsers in Selenium WebDriver?

We can launch different browsers in Selenium WebDriver using several methods. For example, we can use the setWebDriver() method to specify the path to the browser's executable file. Alternatively, we can use the addCustomProfilePreference() method to add a custom profile preference for the browser. Finally, we can use the launchBrowser() method to launch the browser.


2. What is native app?

Those Apps are written by using Android SDKs and IOS are known as Native Apps.

3. What are Mobile Web Apps?

Ans: There are mobile web pages that are those web apps that are accessed with mobile browsers.

4. What is appium?


The tests of Appium are written in any language and this is because appium is nothing but an HTTP server. It is also important that the test should be interfaced with Appium and it uses HTTP libraries so that they can create HTTP sessions.

5.. Write the advantages of using Appium?

Ans: The advantages of Appium are listed below:

  1. Using the same API, Appium will allow you to write tests that are against mobile platforms.
  2. By using any kind of test framework or language you can write and run the tests.
  3. Appium is an open-source platform so you can contribute to it easily.
  4. For the hybrid mobile applications and Native, Appium provides cross-platform.
  5. Appium supports the JSON wire protocol.
  6. Appium does not require recompilation of App.
  7. Appium also supports automation tests on the physical devices and also for simulator or emulator both.
  8. Appium does not have any dependency on mobile devices.

9. What are the disadvantages of Appium?

Ans: The disadvantages of Appium are listed below:

  1. The testing of that android that is lower than 4.2 is not allowed.
  2. Appium has limited support for hybrid app testing. You will not be able to test the action that allows switching of applications from native to web app and from web app to native. 
  3. There is no support that will allow you to run an Appium Inspector on Microsoft Windows.

11. Name the Appium inspector?

Ans: Just like a selenium IDE playback and record tool, Appium consists of an inspector that is used to record and playback. With the help of this, you can record and play native application behavior which is achieved by inspecting DOM. It helps in generating the test scripts in any language that is preferred. But Appium Inspector is not good support Windows and they use UIAutomator viewer in the option.

30. Give the difference between emulator and simulator?

Ans: The simulator is used for calling IOS virtual devices that will launch from Xcode in MAC. The emulator is used for calling Android virtual devices.

Comments

Popular posts from this blog

How the youth Misusing the indian law