InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
In Selenium WebDriver, how do you use the Recovery Scenario? |
|
Answer» Within Selenium WebDriver Java tests, by using "Try Catch BLOCK." try { driver.get("www.interviewbit.com");}catch(Exception e){ System.out.println(e.getMessage());}
|
|
| 2. |
How does Selenium WebDriver handle hidden elements? |
|
Answer» Using the JavaScript executor, we can DEAL with hidden ITEMS. (JavascriptExecutor(DRIVER)).executeScript("document.getElementsByClassName(ElementLocator).CLICK(); |
|
| 3. |
How can I use WebDriver to mouse hover over a web element? |
|
Answer» We can use the ACTIONS CLASS to mouse hover over a web element as shown below in the code SNIPPET: WebElement ele = driver.findElement(By.XPATH("xpath"));//Create object 'action' of an Actions classActions action = new Actions(driver);//Mouseover on an elementaction.moveToElement(ele).perform(); |
|
| 4. |
In Selenium WebDriver, how do I push the ENTER key on a text box? |
|
Answer» To UTILIZE Selenium WebDriver to hit the ENTER key, we MUST use Selenium Enum KEYS with the constant ENTER. driver.findElement(By.xpath("xpath")).sendKeys(Keys.ENTER); |
|
| 5. |
How do I use Selenium WebDriver for submitting a form? |
|
Answer» For submitting a form in SELENIUM WEBDRIVER, we USE the "SUBMIT" method on the element. driver.findElement(By.id("form")).submit(); |
|
| 6. |
How can I use Selenium WebDriver for clicking on a hyperlink? |
|
Answer» In SELENIUM, we use the click() method for clicking on a hyperlink. driver.findElement(By.linkText("Interview BIT Website")).click(); |
|
| 7. |
How do I retrieve the value of an attribute in Selenium WebDriver? |
|
Answer» USING the getAttribute(value) method. It RETURNS the value of the parameterized ATTRIBUTE. HTML: <input name="nameSeleniumWebDriver" value="valueSeleniumWebDriver">Interview Bit</input>SELENIUM Program: String attributeValue = driver.findElement(By.name("nameSeleniumWebDriver")).getAttribute("value");System.out.println("AVAILABLE attribute value is :"+attributeValue);Output: valueSeleniumWebDriver |
|
| 8. |
What is the best way to acquire the textual matter of a web element? |
|
Answer» The best WAY to acquire the textual matter of a web element is by employing the getText() method as shown below: PACKAGE interviewBit;// package nameimport org.openqa.selenium.By;import org.testng.annotations.TEST;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.WebDriver;// importing the necessary libraries// Test classpublic class Test {@Testpublic void testmethod(){// sets the property as requiredSystem.setProperty("webdriver.chrome.driver","C:\\Selenium Environment\\Drivers\\chromedriver.exe");//Creates a new Chrome Web DriverWebDriver driver = new ChromeDriver();driver.get("HTTPS://www.youtube.com");STRING textAvailable=driver.findElement(By.xpath("//*[@id='gbw']/div/div/div[1]/div[1]/a")).getText(); System.out.println("Textual Matter which is Present is :" + textAvailable);}} |
|
| 9. |
How can I use Selenium WebDriver to clear the text in a text box? |
|
Answer» The above TASK can be done using the clear() function as shown below: WebDriver driver = NEW FirefoxDriver();driver.get("https://www.youtube.com");driver.findElement(By.xpath("xpath_of_element1")).sendKeys("Interview BIT");driver.findElement(By.xpath("xpath_of_element1")).clear(); |
|
| 10. |
How can I type text into the text box without using the sendKeys() function? |
|
Answer» We can use the following piece of CODE to TYPE text into the text box WITHOUT using the sendKeys() function: // To INITIALIZE js objectJavascriptExecutor JS = (JavascriptExecutor)webdriver;// To ENTER usernameJS.executeScript("document.getElementById('User').value='InterviewBit.com'");// To enter passwordJS.executeScript("document.getElementById('Pass').value='tester value'"); |
|
| 11. |
How do I use Selenium WebDriver to enter text into a text box? |
|
Answer» Using the method sendKeys() WebDriver DRIVER = new FirefoxDriver();driver.get("https://www.youtube.com");driver.findElement(By.xpath("xpath")).sendKeys("INTERVIEW Bit"); |
|
| 12. |
In Selenium WebDriver, how do you take a screenshot? |
|
Answer» During the execution of the test scripts, test cases MAY fail. We just capture a screenshot and save it in a result repository while manually executing the test cases. Selenium WebDriver can be used to accomplish the same thing. Some of the instances in which we MIGHT need to USE Selenium WebDriver to capture a screenshot are as follows:
TakesScreenshot is a Selenium interface that has a function getScreenShotAs that may be used to take a screenshot of the programme under test. When capturing screenshots with Selenium 3, we may RUN into a few difficulties. We utilize the aShot() function to get around this. |
|
| 13. |
In a Selenium Script, what happens if you use both Thread.Sleep and WebDriver Waits? |
|
Answer» The Thread.sleep() method allows you to suspend the execution for a specified amount of time in milliseconds. If we use WebDriver WAITS in conjunction with the Thread.sleep() method, the webdriver will PAUSE the execution for the provided amount of time in the parameter of the Thread.sleep() function before PROCEEDING to the next wait. If we combine both waits, the TEST execution time will increase. |
|
| 14. |
In a Selenium script, what happens if you use both implicit and explicit wait? |
|
Answer» According to the official SELENIUM manual, mixing Implicit and Explicit Waits is not recommended. Combining the two can RESULT in unpredictable wait times. Only one time in the code is implicit wait specified. Throughout the driver object instance, it will REMAIN the same. Explicit wait is used in the code WHENEVER it is required. At the time of execution, this wait will be called. It's a conditional waiting PERIOD. Wherever explicit wait is applied, it will supersede the implicit wait. As a result, Explicit Wait takes precedence over Implicit Wait. |
|
| 15. |
What is the best way to deal with StaleElementReferenceException? |
|
Answer» Before we look at how to manage a StaleElementReferenceException using the Page Object Model, let's have a look at how to handle a StaleElementReferenceException. First, let's define Stale Element REFERENCE EXCEPTION. Stale refers to something that is old, DETERIORATED, and no longer fresh. An old or no longer AVAILABLE element is referred to as a stale element. Assume there is a WebElement in WEBDRIVER that is discovered on a web page. The WebElement becomes stale when the DOM changes. The StaleElementReferenceException is thrown when we try to interact with a stale element. |
|
| 16. |
What kinds of Selenium WebDriver exceptions have you run into? |
|
Answer» In the following project the exceptions we've run into are as follows:
On the element's, with which we are interacting, destruction and then RESTORATION, we get a stale element reference exception. When this happens, the element's DOM reference becomes invalid. Because of this, we are unable to obtain the element's reference. The following are some more common exceptions:
|
|
| 17. |
As seen below, we establish a WebDriver reference variable called 'driver.' What exactly is the purpose of proceeding in this manner? |
|
Answer» WebDriver driver = NEW FirefoxDriver(); instead of creating FirefoxDriver driver = new FirefoxDriver();We may USE the same driver VARIABLE to work with any BROWSER we want, such as IEDriver, SafariDriver, and so on, if we construct a reference variable of TYPE WebDriver. |
|
| 18. |
At a bare minimum, how many parameters do selenium commands have? |
|
Answer» The FOLLOWING four parameters need to be passed in Selenium: 1. HOST: This is the parameter that binds Selenium to a particular IP address. Because we usually perform Selenium tests on our local system, the value will be 'localhost.' Instead of localhost, you can specify an IP address. The Syntax is as follows: JAVA -jar <selenium server standalone jar name> -host <Your IP Address>2. Port number: TCP/IP port for connecting Selenium tests to the Selenium Grid Hub. 4444 is the default port hub. The Syntax is as follows: java -jar <selenium server standalone jar name> -role hub -port 4444Assure this port isn't being used by any other software on your machine. An exception like Exception in thread "main" java.net may occur. Selenium is ALREADY running on port 4444. BindException: Selenium is already running on port 4444. Alternatively, some other service is available. If this happens, you may either kill the other process using port 4444 or tell Selenium-Grid to USE a new port for its hub. If you want to change the hub's port, the -port option can be used. 3. Browser: For the execution of the selenium scripts i required browser is passed. 4. Url: The url of the application needs to be passed. |
|
| 19. |
Which implementation of WebDriver promises to be the fastest? |
|
Answer» HTMLUnitDriver is the quickest WebDriver implementation because the HTMLUnitDriver does not run tests in the browser, this is the case. When compared to RUNNING the scripts without a browser, STARTING a browser and performing test CASES took longer. For test case EXECUTION, HTMLUnitDriver USED a simple HTTP request-response method. |
|
| 20. |
In Selenium WebDriver, how do you handle Ajax calls? |
|
Answer» When using Selenium WebDriver, one of the most prevalent challenges is handling AJAX calls. We would have no way of knowing when the AJAX call would COMPLETE and the page would be refreshed. In this tutorial, we'll look at how to use Selenium to handle AJAX calls. AJAX (Asynchronous JavaScript and XML) is an acronym for Asynchronous JavaScript and XML. AJAX allows a web page to obtain little quantities of data from the server without having to completely reload the page. Without reloading the page, AJAX sends HTTP requests from the client to the server and then processes the server's answer. Wait COMMANDS may not work with AJAX controls. It's only that the page itself is not going to refresh. The essential information may show on the web page without refreshing the browser when you click on a submit button. It may load in a fraction of a second, or it may take longer. We have no control over how long it takes for pages to load. In Selenium, the easiest way to deal with circumstances like this is to employ dynamic waits (i.e. WebDriverWait in combination with ExpectedCondition) 1. titleIs() – The anticipated condition looks for a specific title on a page. wait.until(ExpectedConditions.titleIs("Big Sale of the Year"));2. elementToBeClickable() – The desired condition requires that an element be clickable, which means that it must be present/displayed/visible on the screen and enabled. wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));3. alertIsPresent() – The expected condition anticipates the APPEARANCE of an alert box. wait.until(ExpectedConditions.alertIsPresent())!=null);4. textToBePresentInElement() – The anticipated condition looks for a STRING pattern in an element. wait.until(ExpectedConditions.textToBePresentInElement(By.id("text"),"text to be found"); |
|
| 21. |
What are the drawbacks of using Selenium for testing? |
|
Answer» The drawbacks of using Selenium for testing are as follows:
|
|
| 22. |
What is the implementation of WebDriver Listeners? |
|
Answer» The Webdriver Event Listeners can be implemented in one of two ways:
|
|
| 23. |
What are Selenium WebDriver Listeners? |
|
Answer» Selenium WebDriver Listeners, as the name implies, "listen" to any event that the Selenium code specifies. Listeners are useful when you want to know what happens before you click any element, before and after you NAVIGATE to an element, or when an exception is THROWN and the test fails. Listeners can be used in Selenium AUTOMATION Testing to log the ORDER of activities and to CAPTURE a screenshot whenever an Exception is thrown. This makes debugging easier in the later stages of Test Execution. Some examples of Listeners are Web Driver Event Listeners and TestNG. |
|
| 24. |
What are the components of Selenium Suite? |
|
Answer» The components of Selenium Suite are as follows:
|
|
| 25. |
How does a Selenium WebDriver interact with the browser? |
|
Answer» On a high level, the Selenium webdriver communicates with the browser and does not transform commands into Javascript. Our Java or Python code will be TRANSMITTED as an api get and post request in the JSON wire PROTOCOL. The browser webdriver interacts with the real browser as an HTTP Request, as mentioned in the previous answer. To receive HTTP requests, each Browser Driver utilizes an HTTP server. When the URL reaches the Browser Driver, it will send the request via HTTP to the real browser. The commands in your Selenium script will be executed on the browser after this is completed.
|
|
| 26. |
Mention the several types of navigation commands that can be used? |
|
Answer» The several types of navigation commands that can be used are as follows:
|
|
| 27. |
What are the different types of waits that WebDriver supports? |
|
Answer» 1. Implicit Wait: Implicit wait instructs Selenium to wait a specified amount of time before throwing a "No such element" exception (One of the WebDriver Exceptions is NoSuchElementException, which occurs when the locators indicated in the Selenium Program code are unable to locate the web element on the web page). Before throwing an exception, the Selenium WebDriver is told to wait for a particular amount of time. WebDriver will wait for the element after this time has been set before throwing an exception. Implicit Wait is activated and remains active for the duration of the browser's open state. It's default setting is 0, and the following protocol must be used to define the specific wait duration. Its Syntax is as follows: driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);2. Explicit Wait: Explicit wait tells the WebDriver to wait for specific conditions before throwing an "ElementNotVisibleException" exception. The Explicit Wait command tells the WebDriver to wait until a certain CONDITION occurs before continuing to execute the code. Setting Explicit Wait is crucial in circumstances when certain items take longer to load than others. If an implicit wait command is specified, the browser will wait the same amount of time before loading each web element. This adds to the time it takes to run the test script. Explicit waiting is smarter, but it can only be used for specific parts. It is, nonetheless preferable to implicit wait since it allows the programme to stop for dynamically LOADED Ajax items. Its Syntax is as follows: WebDriverWait wait = new WebDriverWait(WebDriver Reference, TimeOut);3. FLUENT Wait: It is used to inform the WebDriver how long to wait for a condition and how OFTEN we want to check it before throwing an "ElementNotVisibleException" exception. In Selenium, Fluent Wait refers to the maximum amount of time that a Selenium WebDriver will wait for a condition (web element) to become visible. It also specifies how often WebDriver will check for the presence of the condition before throwing the "ElementNotVisibleException." To put it another way, Fluent Wait searches for a web element at regular INTERVALS until it timeouts or the object is found. When engaging with site items that take longer to load, Fluent Wait instructions come in handy. This is a common occurrence with Ajax apps. It is possible to establish a default polling period while using Fluent Wait. During the polling time, the user can configure the wait to ignore any exceptions. Because they don't wait out the complete duration defined in the code, fluent waits are also known as Smart Waits. Its Syntax is as follows: Wait wait = new FluentWait(WebDriver reference).withTimeout(timeout, SECONDS).pollingEvery(timeout, SECONDS).ignoring(Exception.class); |
|
| 28. |
How to create an Object Repository in your project? |
|
Answer» There is an Object Repository notion in QTP (Quick Test Professional). By default, when a user records a test, the objects and their properties are saved in an Object Repository. This Object Repository is used by QTP to playback scripts. There is no default Object Repository concept in Selenium. This isn't to say that Selenium doesn't have an Object Repository. Even if there isn't one by default, we could make our own. Objects are referred to as locators in Selenium (such as ID, Name, CLASS Name, TAG Name, LINK Text, Partial Link Text, XPath, and CSS). A collection of objects is referred to as an object repository. Placing all the locators in a separate file is one technique to construct an Object Repository (i.e., properties file). The ideal method, however, is to use Page Object Model. Each web page is represented as a class in the Page Object Model Design Pattern. A class contains all of the items associated with a specific page of a web application. |
|