1. What is a page object model?
- It is a design pattern in which we can create separate classes representing each web page
- These classes encapsulate elements and interactions
- The main purpose is to improve the maintainability
2. What is parametrization in Selenium?
Test scripts can be executed multiple times with different data sets.
3. What is the difference between type() and typeskeys() methods?
typeKeys() : Emulate actual user typing
type(): Programmatically populate values
4. What are the 4 parameters that have to pass on Selenium?
- Host
- Port number
- URL
- Browser
5. What is an object repository?
It is a centralized location for web element objects. It is achieved by using a page object model.
6. What is the Selenium grid?
- It is used to do the distributed testing. It consists of a hub (a centralized server) which distributes the load within nodes (other machines).
- Using the Selenium grid we can achieve parallel testing by executing test scripts in different machines with different platforms, browsers etc…
7. What is the dependency used to read data like CSV or Excel files?
Apache POI
8. How to perform multiple keyboard actions using selenium?
sendKeys(keys.SHIFT, “P”);
sendKeys(keys.chrod(keys.SHIFT, "P"));
9. What are the selenium components?
- Selenium Web Driver
- Selenium GRID
- Selenium IDE
- Selenium RC
10. How to create a test framework from scratch?
- Define objectives and requirements
- Identify testing tools
- Select a programming language
- Design framework architecture
- Set up version control system
- Create folder structure
- Implement core functions
- Set up the CI/CD process
- Write test scripts
11. Can we interact with other elements when a modal dialogue appears?
No. Focus goes to the modal dialogue so we cannot interact with other elements on the web page.
12. Write code to read data from the CSV file
//Create a CSVReader to read data from the CSV file
CSVReader reader = new CSVReader(new FileReader("data.csv"));
// Read and process the data from the CSV file
String[] nextLine;
while ((nextLine = reader.readNext()) != null) { String username = nextLine[0];
String password = nextLine[1];
// Find the input fields and populate them
driver.findElement(By.name("username")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
13. Does Selenium support web cam image-taking functionality directly?
No. External libraries may be needed to handle web cam functionalities. anyway, after taking web images, selenium can interact.
14. How to perform parallel test execution in Selenium?
- Need to use a Framework like TestNG
- Can use Selenium grid to perform parallel execution among multiple devices, platforms, and browsers.
15. How to handle broken links?
- Broken links are called dead links
- They do not redirect to the web page they are supposed to
- This happens when the website does not exist or the web page is down
- When someone clicks on the web page an error message appears
- We can use HTTP status code to verify the status of broken links
- if it is valid it represents 2xx status codes
- 400 bad requests — server cannot respond since URL is not correct
- 400 bad request-host is not correct
- 404 not found — the web page is not available on the server
- 503 — service unavailable — service is temporarily overloaded or cannot process the request
16. How to send ALT, SHIFT, and CONTROL keys on the web driver?
//Method 01
sendKeys(Keys.chord(Keys.ALT, Keys.SHIFT,Keys.Control));
//Method 02
Actions a = new Actions(driver)
a.keyDown(Keys.ALT).keyDown(Keys.SHIFT) .keyDown(Keys.CONTROL).sendKeys('test') .build() .perform();
17. How do you send text input to a focused element?
- sendKeys() method automatically focuses the element
- sendKeys() method
18. What does a hybrid framework mean in selenium?
A combination of keyword-driven and data-driven can be an example
19. What is session handling in selenium?
- starting and stopping a session means opening and closing the browser
- A single running instance of a browser that is controlled through WebDriver commands is called a session.
- A new session is created by initializing a driver object
20. How to implement data-driven testing?
- Data-driven testing involves executing the same script with multiple sets of data
- Cucumber is a framework used to implement data-driven testing
21. What is Ajax calls?
Ajax calls allow a website to update part of its content upon a user action without the need to reload the page.
22. What is the same origin policy?
- The same origin policy is introduced due to security reasons
- The internal content of our site never be accessible by other site
- Code loaded within the browser can only be operated by the same domain.
23. How to find broken images in selenium?
//We find all image elements on the page using
driver.findElements(By.tagName("img")).
//For each image, we extract the src attribute, which contains the image URL
String imageUrl = image.getAttribute("src");
//Use below class
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
//If the response code is not HTTP_OK (200),
//we consider the image broken and print a message.
24. How to do a recovery scenario?
- Recovery might involve navigating to a different page
- refreshing the current page
- logging in again
- other appropriate actions to bring the application back to a known state
- Selenium doesn’t have built-in recovery scenarios.
- you can implement your recovery mechanisms using Java’s exception handling and additional logic
//We attempt to find and click on an element (missingElement) that doesn't
//exist on the page, leading to a NoSuchElementException.
//We catch the exception using a try-catch block,
//allowing us to handle the exception.
//In the catch block, we print a message and perform recovery actions,
// such as navigating to another page or logging in again.
//The finally block ensures that cleanup or final actions
//(like closing the browser) are performed regardless of whether
//an exception occurred.
25. How to debug test cases?
Debugging in IntelliJ IDEA:
- Set Breakpoints:
- Open your Java Selenium script in IntelliJ IDEA.
- Identify the line(s) where you want to start debugging.
- Click in the left gutter next to the line number to set a breakpoint.
- A red dot should appear.
- Run in Debug Mode:
- Right-click on your script.
- Select “Debug ‘YourScriptName’.”
- Debug Tool Window:
- IntelliJ IDEA will open the “Debug” tool window.
- The execution will stop at the breakpoints.
- Use Debugging Controls:
- Use the debugging controls in the toolbar (e.g., Resume Program, Step Over, Step Into, Step Out) to navigate through the code.
- Inspect Variables:
- While debugging, you can hover over variables to see their current values
- Open the “Variables” view to inspect variables and their values.
26. Extract the URL of src in all images
public class ExtractImageUrls {
public static void main(String[] args) {
// Set the path to the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Create an instance of the ChromeDriver
WebDriver driver = new ChromeDriver();
// Open the URL
driver.get("https://the-internet.herokuapp.com/broken_images");
// Find all image elements on the page
java.util.List<WebElement> images = driver.findElements(By.tagName("img"));
// Extract and print the URLs of the src attributes
for (WebElement image : images) {
String imageUrl = image.getAttribute("src");
System.out.println("Image URL: " + imageUrl);
}
// Close the browser
driver.quit();
}
}
27. How can you use regular expressions in Selenium?
- Regexp is a keyword that can be prefixed to a text, to treat it as a regular expression in Selenium.
- Matching Text Patterns: Regular expressions can be used to match specific patterns within strings, allowing you to verify expected text in web elements. For example, to validate an email address entered in a form, you can use the following regular expression
- Handling Dynamic Elements: Web pages often contain dynamic elements with changing IDs, classes, or other attributes. Regular expressions can help identify and interact with such elements. For example, if the ID of a button changes dynamically but always starts with “btn_submit_”, you can locate it using a regular expression like this:
28. You are given a set of automated scripts that are highly confidential and must be run on the client’s remote server only. You are asked to modify the scripts based on privacy needs, what will you do?
I understand that this scenario requires the tests to be run on a separate machine. Instead of using the WebDriver library and commands, I will use the RemoteWebDriver command to execute these tests. To run a remote WebDriver client, we need to configure it. This can be done by pointing the URL to the address of the remote server to execute the tests. Capabilities can be added to configure the tests further. Here is a sample code:
//RemoteWebDriver class has multiple constructors
//below is one of them
//It takes URL and capabilities as arguments
// Here we set capabilities as host, url, port, browser name
System.setProperty("Service_name", "selenium_java_client");
System.setProperty("Service_host", "localhost");
System.setProperty("Service_port", "14258");
ImmutableCapabilities = new ImmutableCapabilities("browserName", "chrome");
WebDriver driver=new RemoteWebDriver(new URL("http//sample.com"), capabilities);
29. How is network data capturing done in Selenium?
Network data capturing is done using an API in the default Selenium class. The response includes headers, status codes, timings, and AJAX requests. For instance, Selenium. start(“captureNetworkTraffic=true”) should be started before launching the application. After reaching a point where network traffic is to be captured, you can fire the method:
selenium.captureNetworkTraffic("xml");
//You can also pass plain or JSON as a parameter to this method.
30. How can you debug the tests in the Selenium IDE?
Primarily, we need to insert the breakpoints where we need to debug and execute the test step by step. Next, run the test case, and we can find that the execution stops at the point where the breakpoint is kept. Click on the run button to continue executing the commands sequentially.
31. How to verify the specific position of a web element?
The commands “ verifyElementPositionLeft”, “verifyElementPositionRight”, “verifyElementPositionTop”, “verifyElementPositionBottom” etc can be used to identify the position of the element concerning the web page. The commands use pixel comparison to verify the positions.
32. Which type command should you use if there is a reload event once your typing is completed?
The typeAndWait command can be used when there is a reload event in the software, post a typing action. This is the Selenium IDE method
33. The Selenium script runs in Chrome but not in IE. What can be done?
The following steps can be done to fix this issue:
- Update the Selenium IE Driver
- Verify that the IE driver and working environment are compatible.
- Configure the IE driver with the setProperty method and import dependencies.
- Set the same value for the ‘Enable Protected Mode’ option for all zones from the Security tab
- Turn off the internet security settings in IE when running the script
- Use CSS Selectors to minimize exceptions
- Set a registry entry
- Avoid declaring the driver instance as static for running scripts on browsers parallelly.
- Use the latest Selenium jars
- Enable Javascript on the IE browser.
- Try using JavaScriptExecutor instead of a native click when clicking elements.
- WebDriver Configuration:
- Ensure that you have the correct version of the WebDriver for Firefox. The WebDriver version should be compatible with the version of Firefox you are using.
- Download the latest GeckoDriver (Firefox WebDriver) from the official Mozilla GitHub page: https://github.com/mozilla/geckodriver/releases
- Browser Version Compatibility:
- Check the compatibility between the Selenium version, WebDriver version, and the version of Firefox. There might be compatibility issues if one component is outdated.
- Update Selenium, WebDriver, and Firefox to their latest versions.
- Browser Profile Settings:
- Firefox may have different default settings or configurations compared to Chrome. Ensure that your test script is not relying on specific Chrome settings that may not be applicable to Firefox.
- Firefox may require additional configurations, such as handling security pop-ups or notifications. Adjust your test script accordingly.
- WebPage Loading Delays
- Browser-Specific Code
- Browser Extensions/Plugins
- Page Rendering Differences
- Headless Mode
34. What is the usage of the commons-io plugin?
copy file from the src folder to the destination folder
For the latest updates about selenium visit the official website
https://www.selenium.dev/selenium/docs/api/java/deprecated-list.html
35. Why we use selenium
- It is open-source and free
- It has a larger user base and community support
- Cross-browser support - It supports almost all browsers
- It has great platform compatibility
- Support multiple programming languages
- c#
- Java
- Ruby
- Python
36. What is the Page Object Model in Selenium
- The page object model is a design pattern
- Every web page and corresponding page class which locates elements of the web page
- Page object model increases the usability and readability
No comments:
Post a Comment