3.interview Prep - Selenium
3.interview Prep - Selenium
What is Xpath?
● Xpath is used to find the location of any element on a webpage using html structure.
● We can navigate through elements and attributes in an XML document to locate
webElements such as textbox, buttons, checkbox, etc. in web Page
What is “Thread.sleep()”?
● It is part of Java, not Selenium. It holds the execution of the code for the given time.
● Unlike, Implicit or Explicit wait, it will wait for 10 (given time) seconds even if the element
is found or loaded in 3 seconds / earlier.
● It is not suggested to use Thread.sleep().
● It throws an exception so you must handle or throw it.
normal browser, nothing will appear on the screen when you start up a headless browser,
since the programs run at the backend. This is faster.
● In my project, I include headless browsers in the Driver Util class.
switch (browser) {
case "chromeHeadless":
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(new ChromeOptions().setHeadless(true));
break;
case "firefoxHeadless":
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver(new FirefoxOptions().setHeadless(true));
Break;
case "PhantomJS": // this is a 3rd party tool
WebDriverManager.phantomsjsdriver().setup();
driver = new PhantomJS();
break;
case "htmlUnitDriver": // this is from Selenium
driver = new HtmlUnitDriver();
break;
default:
throw new RuntimeException("Illegal browser type!"); }
How can you find the number of the checkboxes and checked boxes?
● Find the common attribute for all of the checkboxes.
● Store all of the checkboxes (each of them are separate objects) in one single list object
/element by using findElements().
● Use .size() method in the findElements().size() method. // this is for the size
● Create a for-loop and put count++ in it.
● Use isSelected() method with an if statement and if it gives “true” it will increment the
value of count.
● Print the count after the loop.
findElement vs findElements?
● FindElement():
This method returns the first WebElement and gives Exception if the element is not found.
It will return only ONE element.
● FindElements():
This method returns List <WebElement> but does not give Exception if the element(s) is
not found as a result list has null values.
● When Testing a web application using a selenium web driver, we may need to create,
update or delete a cookie.
● For example, when automating Online Shopping Application, we may need to automate
test scenarios like place order, View Cart, Payment Information, order confirmation, etc.
● If cookies are not stored, we will need to perform login action every time before we
execute above listed test scenarios. This will increase your coding effort and execution
time.
● The solution is to store cookies in a File. Later, retrieve the values of cookie from this file
and add to it your current browser.
● session. As a result, you can skip the login steps in every Test Case because your driver
session has this information in it.
● The application server now treats your browser session as authenticated and directly
takes you to your requested URL.
● An SSL (Secure Sockets Layer) certificate is a digital certificate that authenticates the
identity of a website and encrypts information sent to the server using SSL technology.
● Case:
You might receive a notification window stating that your website is not secure and to be
able to complete your automation, you will need to hit the “proceed anyway” button.
● There are different certifications and each may have different ways of accepting and
continuing the process. You can handle this with DesiredCapabilities or ChromeOptions
classes.
● Syntax: (for general chrome profile)
DesiredCapabilities ch = DesiredCapabilities.chrome();
ch.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
ch.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty(“webdriver.chromeDriver”, “ ”);
WebDriver driver = new ChromeDriver(c);
// this is how we pass capabilities into the “driver so it accept all
certifications when they appear on the screen.
Selenium :: 13
In some cases, maximize() will not work>so what will be the way around?
➔ ChromeOptions options = new ChromeOptions();
options.addArguments("startmaximized");
How to input text in the text box without calling the sendKeys()?
How would you verify the position of the Web Element on the page?
➔ WebElement class has a get Location method with returns the top left corner of the element
element.getLocation();
Selenium :: 16
● HUB:
○ The hub is the central point that will receive all the test requests and distribute
them to the right nodes.
○ There should be only one hub in a grid.
○ The machine containing the hub is where the tests will be triggered, but you will
see the browser being automated on the node.
○ The hub can be parametrized with a json file.
● NODE:
○ Nodes are the Selenium instances that will execute the tests that you loaded on
the hub.
○ Nodes can be launched on multiple machines with different platforms and
browsers.
○ Nodes can be parametrized with a json file.
Selenium :: 18
2. Register your hub by executing the below code in the command prompt:
java -jar selenium-server-standalone-<version>.jar -role hub
1. C:\Users\ozzy\OneDrive\Desktop\Selenium>
2. java Dwebdriver.chrome.driver="C:\Users\ozzy\OneDrive\Desktop\Selenium\chromedriver.exe"
3. jar seleniumserverstandalone3.14.0.jar
4. role webdriver
5. hub http://192.168.2.21:4444/grid/register
6. port 5566
❖ By default, starting the node allows for concurrent use of 11 browsers... : 5 Firefox, 5
Chrome, 1 Internet Explorer. The maximum number of concurrent tests is set to 5 by
default.
❖ To change this and other browser settings, you can pass in parameters to each
-browser switch (each switch represents a node based on your parameters). If you use the
-browser parameter, the default browsers will be ignored and only what you specify on the
command line will be used.
❖ -browser browserName=firefox,version=3.6,maxInstances=5,platform=LINUX
Thread is like one process or instance of application run and there are 4ways for parallel testing:
AWS
What is AWS?
● AWS is providing cloud VM. Create an EC2 instance.
● I can use this instance with a remote desktop. Actually, after launching my instance I just
use it like a regular computer.
:::::Parallel testing:::::
To perform parallel testing, we need to configure the webdriver properly. If we are using a
singleton driver, the only way to do parallel testing is to use forks. Fork, it's one JVM
process.##Forks require a lot of cpu and ram, that's why it's not recommended to use. In our
project, we use ThreadLocal driver.ThreadLocal allows to create a copy of the webdriver object
during the run time. For every feature file that we run, threadlocal will create the webdriver copy.
Also, we are using maven-surefire plugin where we can specify maximum number of
threads:<parallel>methods</parallel><perCoreThreadCount>false</perCoreThreadCount><thr
eadCountMethods>4</threadCountMethods>1. webdriver = test scenario = 1 browser
ThreadLocal class allows us to avoid forks.