DEV Community

Cover image for Selenium Automation Testing  ๐Ÿ”งโš™:
Rahul Kumar
Rahul Kumar

Posted on โ€ข Edited on

1 2

Selenium Automation Testing ๐Ÿ”งโš™:

What is Selenium :

  • selenium was first created by Jason Huggins in 2004
  • Selenium is a software testing tool which can automate your entire testing phase .

Components of Selenium :

Selenium is a package which has different components for different use cases .

  • Selenium RC It is an web server which acts as an HTTP proxy .
  • Selenium IDE It's just a plugin . Mainly used for recording and playback the script .
  • Selenium Grid Multiple test cases can be executed simentaniously.
  • Selenium WebDriver Main component of selenium which used for cross-platform testing .

Features :

  • Open source .
  • Easy to implement .
  • less hardware usages .
  • multi browser support .
  • different language support .
  • Parallel test execution .

Set up :

  • Download
  • Download the chrome driver (Different browser have diff driver)
  • Install Selenium standalone server

  • Open eclipse

  1. create a new project
  2. right click on it
  3. Navigate to properties
  4. Build paths
  5. click on classpath library .
  6. Add external jar
  7. Navigate to the folder (Where you have downloaded your selenium click on that and under lib folder we have our jar files(executable files) ) .
  8. Apply and close Alt Text

A simple test case written on java :

public class Lunch {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.drive", "C://selenium//chromedriver.exe");

       //We need to create the object for our chromedriver
       WebDriver driver =  new chromeDriver();

        driver.get("https://www.facebook.com");
       //Then we can perform Desired testing with driver object
       driver.findElement(By.id("email")).sendKeys("hlw");

       driver.findElement(By.id("pass")).sendKeys("1234");


       driver.findElement(By.name("login")).click();
       }
}
Enter fullscreen mode Exit fullscreen mode

So here we are automating the task we might do manually like

  1. opening the browser .
  2. Insert the credentials .
  3. Then click on Log in .
  • What we are defining Selenium is to go to the Facebook's URL and by using selectors .
  • We can find the selectors by inspecting the page (ctl + shift + i).

  • After Entering the credentials for username and password fields we then click on the login button .

How can we optimize more .

public class Lunch {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "C:\\Selinium\\chromedriver.exe");

        //we need create a object for the chromedriver
        WebDriver driver = new ChromeDriver();

        driver.get("https://www.facebook.com");

        String title =  driver.getTitle(); //Actual title

//      Facebook โ€“ log in or sign 

        if(title.matches("Facebook โ€“ log in or sign up")) {
            System.out.println("valid page");

            driver.findElement(By.id("email")).sendKeys("hlw");
            driver.findElement(By.id("pass")).sendKeys("1234");

            driver.findElement(By.name("login")).click();
        }else {
            System.out.println("Invalid page");

            driver.close();
        }
    }

}
Enter fullscreen mode Exit fullscreen mode

Upon the validity of the mentioned URL we are performing the task otherwise simply close the connection .

Alt Text

we can use TestNg for generating the test reports . where NG means "next generation" . It is a testing framework like Junit .

Features of TestNG :

  • annotations are easy to create test cases .
  • Test case can be grouped by prioritized more easily .
  • It supports parameterization .
  • supports data-driven testing using DataProviders .
  • Generates HTML reports .
  • Parallel test execution is possible .

Annotations in TestNG :

Annotations in TestNG are used to decide the flow of the program .

  • @BeforeSuite
  • @BeforeTest
  • @BeforeClass

Why do we need Maven :

  • Imagine a scenario where we want to give the same test case file to other person then we have to also include the jar files which is time taking because sending a lot of jar files over the internet could be inappropriate .
  • Here we could use Maven .

Alt Text

  • Maven is used to download dependencies for a software program .
  • dependencies are downloaded are included inside a POM file .
  • Once the dependencies are included simply save the project and all the dependencies will automatically downloaded .

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free โ†’

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay