There are no items in your cart
Add More
Add More
Item Details | Price |
---|
Learn all the frequently asked interview questions and answers
1. What is Page Object Model(POM)? What are its advantages?
Page Object Model is a design pattern for creating an object repository for web UI elements. Each web page in the application is required to have its own corresponding page class. The page class is thus responsible for finding the WebElements in that page and then perform operations on those web elements.
The advantages of using POM are:
2. Explain the different exceptions in Selenium?
The most common exceptions in selenium are:
3. How do you locate an element by partially comparing its attributes in XPath?
XPath supports the contains() method. It allows partial matching of attribute’s value. It helps when the the attribute uses dynamic value while also having some fixed part.See the below example –
//*[contains(@id,'java')]
4. How do you locate elements based on the text in XPath?
We can call the text() method. The below expression will get the elements that have text nodes that equal ‘Java’
//*[text()='Java']
5. What is the selenium command to delete session cookies?
webdriver.manage.deleteAllCookies();
6. What is the difference between GetWindowHandle() and GetWindowHandles() methods?
webdriver.getWindowHandle() – It gets the handle of the active web page
webdriver.getWindowHandles() – It gets the list of handles of all the pages open at that time
7. How to handle Web based alerts/pop-ups in Selenium?
WebDriver exposes the following methods to handle such popups:
8. Write code to wait for a particular element to be visible on a page.
WebDriverWait wait = new WebDriverWait(driver,20);Element = wait.until(ExpectedConditions.visibilityofElementLocated(By.xpath("")));
JavaScriptExecutor js = (JavaScriptExecutor)driver; js.executeScript("windows.scrollBy(0,500)");
JavaScriptExecutor js = (JavaScriptExecutor)driver; js.executeScript("arguments[0].scrollIntoView();",element);
11. How can you fetch an attribute value from an element in Selenium?
We can fetch the attribute value of an element by using the getAttribute() method. Sample code:
WebElement eLogin = driver.findElement(By.name("Login"));String LoginClassName = elogin.getAttribute("classname");
12. How to retrieve typed text from a textbox?
We can use getText() method to retrieve some text from textbox. S
Sample code:
13. How to take screenshots in Selenium Webdriver?WebElement eLogin = driver.findElement(By.name("Login"));String loginText = eLogin.getText();
File scrFile = ((TakeScreenshot)driver).getScreenshotAs(ouputType.FILE);
14. Can we enter text without using SendKeys() method?
We can do it using JavaScriptExecutor.
JavaScriptExecutor js = (JavaScriptExecutor)driver; js.executeScript("document.getElementById('Login').value=Enter text");
15. How to select a value in a dropdown?
We can select value using Select class. Sample code:
WebElement myElement = driver.findElement(By.name("dropdown"));Select select = new Select(myElement);select.selectByVisibleText(Text);select.selectByIndex(Index);select.selectByValue(Value);
16. What is fluent wait in Selenium?
A fluent wait is a type of wait in which we can also specify polling intervals(intervals after which driver will try to find the element) along with the maximum timeout value.
Wait wait = new FluentWait(driver);.withTimeout(20, SECONDS);.pollingEvery(5, SECONDS);.ignoring(NoSuchElementException.class);WebElement textBox = wait.until(new Function(){public WebElement apply(WebDriver driver){return driver.findElement(By.Id("textBoxId")); }});
17. How to locate a link using its text in Selenium?
Using linkText() and partialLinkText() we can a locate a link. The difference between the two is linkText matches the complete string passed as parameter to the link texts. Whereas partialLinkText matches the string parameter partially with the link texts.
WebElement link1 = driver.findElement(By.linkText("artOfTesting")); WebElement link2 = driver.findElement(By.linkText("artOf"));
18. How to do drag and drop in selenium?
Using Actions class, drag and drop can be performed in Selenium. Sample code:
Actions builder = new Actions(driver);Action dragAndDrop = builder.clickAndHold(SourceElement).moveToElement(TargetElement).release(TargetElement).build();dragAndDrop.perform();
19. How to handle alerts in Selenium?
In order to accept or dismiss an alert box the alert class is used. This requires first switching to the alert box and than using accept() or dismiss() command.
Alert alert = driver.switchTo().alert();alert.accept();alert.dismiss();
20. How to launch a batch file in Selenium Webdriver?
We usually run a batch file or executable file for setting up the prerequisites before starting the automation. You can use the below java code for this purpose:
Process batch = Runtime.getRuntime.exec("path of batch file"); batch.waitFor();
21. What is the process to start Chrome/IE browser?
If you want to start a browser then, just set the system properties as mentioned below:
// For the IE web browserSystem.setProperty("webdriver.ie.driver,"iedriver.exe");WebDriver driver = new InternetExplorerDriver();//For the Chrome web browserSystem.setProperty("webdriver.chrome.driver,"chromedriver.exe file path");WebDriver driver = new ChromeDriver();
22. How would you simulate the right click operation on WebDriver?
You can make use of the Actions class features.
Actions action = new Actions(driver); action.moveToElement(element).perform(); action.contextClick().perform();
FileInputStream fis = new FileInputStream("path of excel file");Workbook wb = WorkbookFactory.create(fis);Sheet s = wb.getSheet("sheetName");// read dataString value = s.getRow(rowNum).getCell(cellNum).getStringCellValue();// write datas.getRow(rowNum).getCell(cellNum).setStringCell("value to be set");FileOutputStream fos = new FileOutputStream("path of file");wb.write(fos); //save file
24. How can you check that element is not present using Selenium?
If the below method returns false than we can assert that element is not present in DOM.
public boolean isElementPresent(By locatorKey) {try { driver.findElement(locatorKey);return true; }catch (org.openqa.selenium.NoSuchElementException e) {return false; } }
25. What is Page Factory and how it is implemented in Selenium?
Page Factory class in Selenium is an extension to the Page Object Design pattern. It is used to initialize the elements of the page object or instantiate the page objects itself.
Annotations in Page Factory are like this:
OR@FindBy(id = “userName”)WebElement txt_UserName;
We need to initialize the page object like this:@FindBy(how = How.ID, using = “userName”)WebElement txt_UserName;
PageFactory.initElements(driver, Login.class);@CacheLookUp – If we know that element is always present on the page, it is best to use this to save some time:
@FindBy(id = "userName")@CacheLookupprivate WebElement txt_UserName;
Bijan Patel
Full Stack Test Automation Expert | Selenium Framework Developer | Certified Tosca Automation Specialist | Postman | DevOps | AWS | IC Agile Certified | Trainer | Youtuber | Blogger|