Table of Contents

 1. What is the difference between Assert and Verify. 

Assert: If the condition fails, then the test will be terminated. There will not be further execution of the test step

Verify: If the condition fails, Execution will continue as normally there will be error logs. Execution will not be terminated.

2. What are Hard and Soft Asserts in selenium?

Hard Assert: Hard assert throws the exception as soon as the condition fails and execution for the current test case will be stopped and continues with the next test case execution.

Soft Assert: Soft Assert doesn’t throw an exception when the condition fails.

(Note: Question 1 and Question 2 are mostly the same the way asked is different)

3. What is the Action Class in Selenium?

Action classes provide the ability to simulate mouse and keyboard events. Example Mousehover, Drag and drop, etc.

Actions action = new Actions(driver);

action.moveToElement(element).click().perform();

4. How can I switch to Multiple Windows in Selenium?

  • Selenium supports working with Multiple Tabs and Windows. switchTo() method is mainly used for Switching between Tabs or windows.

ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());

//switch to new tab

driver.switchTo().window(newTab.get(1));

5. How to handle alerts pop up in Selenium?

driver.switchTo().alert() is used for Switching to Browser Alert.

Accept Alert :

driver.switchTo().alert().accept();

Dismiss Alert:

driver.switchTo().alert().dismiss();

              Get Text:

driver.switchTo().alert().getText();

Type Text:

driver.switchTo().alert().sendKeys("Text");

6. Explain Javascript Executor in Selenium.

Javascript executor helps to execute Javascript on the browser. Sometimes the default simulation events might not work click(), getText(), etc. Using Javascript executor we can make it work.

Example:

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("document.getElementByID('element id ').click();");

7. Can I Create a Selenium Java framework without using TestNG?

TestNG and Selenium are two different things, TestNG helps us to manage Assertions, Reporters and makes things easier for creating the complete framework. We can use other frameworks like Junit to create Selenium Framework.

8. Explain breifly idDisplayeD() isSelected() and IsEnabled command?

IsDisplayed(): This method verifies element is present in the DOM tree and it is displayed. Returns true if displayed else returns false.

isSelected(): This method is mainly used for radio buttons, checkboxes, or options in a menu. This helps to verify if an element is selected status. If selected returns true else returns false.

isEnabled() : This method is used To verify if an element is enabled or disabled on a web page. Returns "true" if an element is enabled and returns "false" if an element is disabled. Mainly used for button elements, locked/disabled text input elements.

9. What is the main disadvantage of implicit wait?

Implicit wait works at the driver level, once you set it wait will be applicable for the whole set of tests, which might slow down the test. Whereas explicit wait works at the element level at it is applicable only for that element.

10. Explain how do you Create a driver instance for Firefox, Chrome, Edge, Safari, and IE?

Creating Driver Instance for Chrome on Windows:

System.setProperty("webdriver.chrome.driver", "path of driver");

WebDriver driver=new ChromeDriver();

Creating Driver Instance for Firefox on Windows:

System.setProperty("webdriver.gecko.driver",Path_of_Firefox_Driver");

WebDriver driver = new FirefoxDriver();

Creating Driver Instance for Safari on Mac:

WebDriver driver = new SafariDriver(); 

Creating Driver Instance for Edge on Windows:

System.setProperty("webdriver.edge.driver", "C://EdgeDriver.exe");

 WebDriver driver = new EdgeDriver();


11. What is the difference between getWindowhandles() and getwindowhandle()?

driver.getWindowHandles() – Returns set of window handles for all the pages opened simultaneously.

driver.getWindowHandle() - Returns  window handle of the web page which is in focus

12. From your test script how you can create HTML test report?

Selenium doesn’t provide any options to create HTML report, however, if we are using TestNG or Junit we can create one.

TestNG: TestNG supports default emailable HTML reports we can use that.

ANT also provides some form of HTML report.

13. How to automate Select Dropdown in Selenium?

Select select =new Select(driver.findElement(By.id("cars")));

select.selectByVisibleText("Honda")

14. What is Selenium Grid?

  • Selenium Grid is used for the execution of WebDriver scripts on remote machines (virtual or real)
  • Selenium Grid allows us to run tests in parallel on multiple machines, and to manage different browser versions and browser configurations centrally

15. Explain Some of the Features of selenium 4.0?

1. Selenium IDE is supported with Chrome, Firefox and Edge.

2. Relative Locators

3. Screenshot Capability

4. Enhanced New Window and New Tab Support

5. Support for Chrome DevTools Protocol (CDP)

16. What is CDP in Selenium?

CDP is a new feature supported by Selenium, CDP means Chrome DevTools Protocol. This feature allows working with Chrome DevTools Programmatically using Selenium.

17. What are Relative Locators in Selenium 4.0?

  • Selenium 4.0 Supports relative locates such as
  • toLeftOf(): Element located to the left of specified element.
  • toRightOf(): Element located to the right of the specified element.
  • above(): Element located above with respect to the specified element.
  • below(): Element located below with respect to the specified element.
  • near(): Element is at most 50 pixels far away from the specified element. The pixel value can be modified.

18. How can work with Network Requests in Selenium 4.0?

Selenium CDP allows us to work with Chrome Network Request directly.

Below is a simple example to capture network requests.

          DevTools devTool = driver.getDevTools();

            devTool.createSession();

            devTool.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

            devTool.addListener(Network.requestWillBeSent(), requestSent -> {

                  System.out.println("Request URL => " + requestSent.getRequest().getUrl());

                  System.out.println("Request Method => " + requestSent.getRequest().getMethod());

                  System.out.println("Request Headers => " + requestSent.getRequest().getHeaders().toString());

                  System.out.println("------------------------------------------------------");

            });

            driver.get("https://rahulshettyacademy.com/#/index");

19. Write a Simple Program to iterate over table.

20. Can I integrate Selenium Tests with CI/CD, name some tools?

  • Yes, Selenium is DevOps friendly framework we can configure to CI/CD tools such as Jenkins, Azure DevOps etc.

21. What is SauceLabs or BrowserStack, How it helps with Selenium Tets?

SauceLabs, BrowserStack etc. provide a virtual environment to execute tests. Using Cloud Testing Tools such as Sauce Labs, BrowserStack we don’t have to set up the physical device. We can run our selenium tests using these tools with different OS, Devices, and Browsers.

22. I need to press Keys CTRL + SHIFT + S  how can I do that in Selenium?         

23. Does Selenium support IFrames, If so please explain.

  • Selenium Supports Iframe, We can Switch To and From Iframe.
  • Example Code
  • We can Switch to Iframe using the below techniques.
  • By Index
  • Example:

driver.switchTo().frame(0);

  • By Name or Id

driver.switchTo().frame("iframe1")

  • By Web Element

WebElement iframe = driver.findElement(By.id("Frame1"));

driver.switchTo().frame(iframe);

24. Write a Small Code Snippet Launch a browser and Navigate to Webpage and Close the browser.

System.setProperty("webdriver.chrome.driver","C://chromedriver.exe");

driver= new ChromeDriver();

 driver.get("https://rahulshettyacademy.com/");


25. How can I Capture Screenshot in Selenium 4?

      //Taking screenshot of WebPage

      WebDriver driver = new ChromeDriver();

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

        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

        FileUtils.copyFile(scrFile, new File("./image.png"));

      //Taking screenshot of WebElement

      WebDriver driver = new ChromeDriver();

      driver.get("https://www.example.com");

      WebElement element = driver.findElement(By.cssSelector("h1"));

      File scrFile = element.getScreenshotAs(OutputType.FILE);

      FileUtils.copyFile(scrFile, new File("./image.png"));

26. Give an example to perform drag and drop action In Selenium WebDriver?

Actions actions = new Actions(driver);

WebElement from = driver.findElement(By.id("column-a "));

WebElement to = driver.findElement(By.id("column-b"));

actions.dragAndDrop(from, to).build().perform();

27. How to handle hidden element in Selenium?

A hidden element means that Element is present in DOM but it is not visible in the browser. To handle the hidden element in Selenium, we need to use the JavaScript Executor.

For Example:

Consider we have a hidden text box with id="hiddenbox1".

JavascriptExecutor executor = (JavascriptExecutor) driver;

String str = (String) executor.executeScript("return document.getElementById('hiddenbox1').value");

Though, the hiddenbox1 is not visible on the browser, since the text box is attached to DOM we can get the value of the text box using the above method.

28. Which method is the overloaded method selenium webdriver?

The most used overloaded method in Selenium is:

  • frame() Method: This method is helpful when we work with iFrame. Based on the parameter we pass Index, WebElement, or name the respective implementations are called.
  • Example:
  • By Index
  • driver.switchTo().frame(0);
  • By Name or Id
  • driver.switchTo().frame("iframe1")
  • By Web Element
  • WebElement iframe = driver.findElement(By.id("Frame1"));
  • driver.switchTo().frame(iframe);

29. How to read data from excel in selenium webdriver?

Selenium Webdriver doesn’t provide any functionality to work with an excel file. However, reading and writing data into excel can be done using Java Utility called POI

Apache POI is an open-source java library used to handle Microsoft Office-based files. This helps the user to perform the operation on excel files such as read, write, etc.

Once you add the Apache POI jar to your project you can use the library utilities to read/write data into Excel File.

Example:

FileInputStream fs = new FileInputStream("D:\\Example.xlsx");

XSSFWorkbook workbook = new XSSFWorkbook(fs);

XSSFSheet sheet = workbook.getSheetAt(0);

Row row = sheet.getRow(0);

Cell cell = row.getCell(0);

System.out.println(sheet.getRow(0).getCell(0));



Tags


You may also like

API Basics and Architecture

API Basics and Architecture
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}