Software Testing - Selenium
Software Testing - Selenium
09.04.2024
─
Overview
Selenium is an open-source, automated testing tool used to test web applications
across various browsers. Selenium can test web applications against various browsers
like Firefox, Chrome, Opera, and Safari, and these tests can be coded in several
programming languages like Java, Python, Perl, PHP, and Ruby.
Selenium is not just one tool or API; it comprises many tools: Webdriver, IDE, Grid.
● WebDriver uses browser automation APIs provided by browser vendors to control
the browser and run tests. This is as if a real user is operating the browser. Since
WebDriver does not require its API to be compiled with application code, it is not
intrusive.
● IDE (Integrated Development Environment) is the tool you use to develop your
Selenium test cases. It’s an easy-to-use Chrome and Firefox extension and is
generally the most efficient way to develop test cases. This is not only a
time-saver but also an excellent way of learning Selenium script syntax.
● Selenium Grid allows you to run test cases in different machines across different
platforms. After the development of the WebDriver tests, you may face the need
to run your tests on multiple browsers and operating system combinations. This
is where Grid comes into the picture.
Selenium removes the need for manual testing by automating regression tests.
This saves testers several days of effort that goes into manually testing every
functionality of the software whenever a change is made to the source code. It is one of
the few testing tools that support a wide range of browsers. It can support all the recent
versions of Google Chrome, Internet Explorer, Opera, Safari, and Mozilla Firefox.
Selenium Suite
Selenium has a dedicated suite that facilitates easy testing of web applications,
comprising the following tools.
● Selenium IDE
● Selenium WebDriver
● Selenium Grid
Selenium IDE
Selenium IDE is part of the Selenium suite and was developed to speed up the
creation of automation scripts. It’s a rapid prototyping tool and can be used by
engineers with no programming knowledge whatsoever. IDE is an easy-to-use interface
that records user interactions on the browser and exports them as a reusable script.
I. Installation
Step 1: Open the Chrome browser.
Step 2: Click on the menu in the top right corner.
Step 3: Click on Extensions in the drop-down menu.
Step 4: Click on Visit Chrome Web Store and type Selenium IDE.
Step 5: Click on Add to Chrome.
3
Once installed, the Selenium IDE icon appears on the top right corner of the
browser. When clicked, a Welcome message appears.
Step 4: The IDE launches the website. Then Sign in to your account and use the
functionality that has to be tested in the website.
Step 5: The Assert command can be used to verify the title or other functionality
of the website.
Step 6: Go back to the IDE window and stop recording.
Playing back
The recorded script can now be executed to ensure that the browser is
working accordingly. Now, the user can monitor the stability and success rate.
5
Selenium WebDriver
Selenium WebDriver was the first cross-platform testing framework that
could configure and control the browsers on the OS level. It served as a
programming interface to create and run test cases. WebDriver performs actions
on web elements. It supports various programming languages like Java, C#, PHP,
Python, among others. It can also be integrated with frameworks like TestNG and
JUnit for test management.
I. Installation
Step 1: Download and install Python3. Once the download is completed,
the Python executable file gets saved in our system. Click on this file, and
the Python installation landing page gets opened. Then, click on Install
Now.
Step 2: Download Pycharm (community edition). Once the download is
complete, click on the executable file. This will open the installation page.
Then, configure according to the system needs and let the installation
finish.
Step 3: Use pip to install the selenium package. Python 3 has pip available
in the standard library. Using pip, you can install selenium using the
following command
pip install selenium
Step 4: Selenium requires a driver to interface with the chosen browser. All
supported browsers will have their own drivers available. For Chrome, the
browser driver can be installed by following the below URL.
https://sites.google.com/chromium.org/driver/
Step 5: Create a new Python package within a new Python project in
Pycharm. Create a new file within the package and start writing your
script.
7
driver.get("https://www.browserstack.com/users/sign_in");
8
3. Navigate to the required webpage: Open the browser with the desired
URL. Use the command below to open the URL in the desired instantiated
browser:
driver.get("https://www.browserstack.com/users/sign_in");
4. Locate the relevant web element: For example, let’s try to locate the email
and password field of the login form of Browserstack sign in page.Inspect
the email field to locate the element using the ID locator, as shown in the
image below:
9
5. Perform action on the web element: After locating the element, testers
need to perform the desired action. In this case, the action is entering text
in the email and password fields and hitting the login button. For this, the
script uses sendKeys and click methods, as shown below:
username.sendKeys("abc@gmail.com");
password.sendKeys("your_password");
login.click();
6. Verify and validate the action: To validate the results, use assertion.
Assertions are important for comparing the expected results to the actual
results. If it matches, the test case passes. If not, then the test case fails.
The syntax below will help to assert the outcome from actions by
performing login automation:
Assert.assertEquals(actualUrl, expectedUrl);
After the test script is complete, it is run from PyCharm to automate the
browser.
After logging in
12
Selenium Grid
Selenium Grid allows to run test cases in different machines across
different platforms. The control of triggering the test cases is on the local
machine, and when the test cases are triggered, they are automatically executed
by the remote machine. It supports distributed test execution. Test cases can be
made to run in parallel in multiple machines on Selenium Grid.
I. Installation
Step 1: Download and install the Java SDK from the official website.
Step 2: Install all the necessary browsers and browser drivers.
Step 3: Download Eclipse IDE to run WebDriver scripts.
Step 4: Download the Selenium Standalone Server from the official
website to run Remote Selenium webdriver.
Step 5: Open Command prompt and type the following command to start
the Selenium server.
java -jar selenium-server-4.19.1.jar standalone
13
Step 6: Create a new project in Eclipse IDE. Then create a new package
and a new class, within which we will write WebDriver scripts. Also add the
downloaded Selenium server jar file while configuring Build Path.
II. Testing
● After starting the selenium server, a local host URL will be created. By
default it is, http://localhost:4444.
● Copy the URL and paste it in your browser to see the sessions that are
running and how many instances of the browser have been created.
14
● Now, you can view the current sessions that are running in the browser, its
start time, the duration and also the node URL.