Home Guide How to run your first Selenium test script?

How to run your first Selenium test script?

Shreya Bose, Technical Content Writer at BrowserStack -

Selenium is the most widely used tool for automation testing of websites and web applications. Therefore, it becomes essential to learn how to code and write the first Selenium test script.

This article will walk the reader through executing their very first test case in Selenium.

Introduction to Selenium

Selenium is the first thing that comes to mind when one is planning to automate the testing of web applications. Selenium is not only open source but also a portable software testing framework for web applications that support multiple languages like Java, C#, Ruby, Python, etc. Choosing the right language depends on the application under test, the supporting community, available test automation frameworks, usability, elegance, and of course, seamless build integration.

Prerequisites for Selenium Installation

Before configuring Selenium, one must configure various other dependencies as well. The following components are required to get started with Selenium automation:

  1. Java (JDK)
  2. Eclipse
  3. Selenium Client and WebDriver Language bindings
  4. Configuring Selenium Webdriver with Eclipse
  5. Creating and Running the first test

Learn how to configure and execute the first test in Selenium using Java with the help of this Selenium with Java article.

How to run the first Selenium Test Script?

All Selenium tests must be run on a Selenium grid. In this article, the test will be run using the cloud Selenium grid offered by BrowserStack.

BrowserStack enables developers and testers to test their websites and mobile applications across 2000 + real browsers, operating systems, desktop, and mobile devices. This is possible for users having to install or maintain an internal lab of virtual machines, devices, or emulators.

Now let’s learn how to write the first Selenium test script.

Run First Selenium Test Script for Free

First, one needs to sign up for a Browserstack account. Next set of steps will go as follows:

Steps to execute the test case:

  1. After logging into the account, choose the Automate tab from the home page.
  2. After that, the user will be navigated to execute your test case as shown below.
    Step 1 - Run Selenium Test Script
  3. Click on Let’s get started!Note: Before using Browserstack, make sure Selenium has been configured on the system.
  4. On signing up for the first time, Browserstack by default will provide an access key and the username. Throughout the test execution, it will remain the same.

Step 2 - Run Selenium Script

As you can see in the snapshot above, while configuring the tests, the user can choose and set the operating system and browser of their choice or necessity.

On the RHS of the snapshot, see how the AUTOMATE_KEY and the URL are being set by default. Also, the desired capabilities and driver settings are configured by BrowserStack. This makes it very easy to write test cases on BrowserStack.

One can check the Capability Generator for the BrowserStack Automate too.

5. Integrate these changes and proceed further to get the entire program/test case in one shot as shown in the snapshot below.

Step 1 - Run Selenium Script using Capability Generator

6. After this, download or copy the code and paste it in the local IDE like Eclipse. One can also execute it directly on the command prompt as shown in the snapshot below.

Step 4 - Run Selenium Script

Note: It is necessary to import or configure Selenium Jar into the project. If Selenium is not configured, it will throw an error. In case someone is using a maven project, add the following dependency into the pom.xml file.

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>

7. In this case, Eclipse IDE has been used to execute the test case. Simply copy the code and paste on the Eclipse platform as shown in the snapshot below.

Here the first test case – a Google Search of “Browserstack”

Run Sample Selenium Script

Code Snippet for the above example:

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;

public class BrowserStackSampleTest {
public static final String USERNAME = "";
public static final String AUTOMATE_KEY = "";
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception {
DesiredCapabilities caps = new DesiredCapabilities();

caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "80");

caps.setCapability("name", "nehapramodvaidya1's First Test");

WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
// Navigating through the URL
driver.get("http://www.google.com");
//Locating the search box of google
WebElement element = driver.findElement(By.name("q"));
// Sending browserstack keyword for search
element.sendKeys("BrowserStack");
element.submit();

System.out.println(driver.getTitle());
driver.quit();
}
}

On executing this selenium test case, the next thing to do is open Browserstack and click on Run the test. The screen below will be displayed:

Final Step to run Selenium Script
Now go to the dashboard to view the test.

8. The user will see the tests and their various features as displayed in the snapshot below.

Dashboard Output
The user can play the test that has been performed. They can also download the test case. Browserstack provides a wide range of capabilities that detail every step in detail along with the time of execution.

Run the test, and start using Selenium on BrowserStack to create error-free, user-friendly websites with minimal time and effort. By following the instructions in this article, take the first step into automation testing success.

Try Running Selenium Test Script for Free

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free