Selenium Automation Testing  🔧⚙:
Tags: selenium, testing, devops
Published: 2020-11-24 09:17:01

# __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__
1. Download the chrome driver (Different browser have diff driver)
2. 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](https://dev-to-uploads.s3.amazonaws.com/i/3mujmndht3tdgr0hz77u.png) 

A simple test case written on java : 
```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();
       }
}
```
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 .
 
```java
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();
		}
	}

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

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vbpqgsmb7r92vo96pvic.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/ig9ohutz8rb2e7kbgoii.jpeg)

* 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 .