How to execute selenium test cases using bat file.

Selenium using bat file

how to Run selenium test cases using bat file.

Step 1: Create a simple Java project and add TestNG class.
Step 2: Write some simple selenium code.

Step 3: Now select option convert to TestNg as shown in below screenshot.

Step 4: Click on finish

Step 5: It will create a testng.xml file as shown below:

Step 6: We are done with project setup steps, Lets create a bat now:
……………………………………………………………………………………………………………………………………..
set projectLocation=G:\JavaPractice\Cmd_Testng
cd %projectLocation%
set classpath=%projectLocation%\bin;%projectLocation%\lib\*
java org.testng.TestNG %projectLocation%\testng.xml
pause
…………………………………………………………………………………………………………………………………….

Step 7 : Double click on bat file to execute the test cases

SELENIUM FRAMEWORK FOR BEGINNERS

Selenium Webdriver Framework01

In this framework we will try to cover all the basic component of a selenium as listed below:

1. TestNg demonstration
2.POM- Page Object Model
3.Code separation
4.Cross Browser Testing
5.External Data Source

Step 1 Create a Java project and Name it as “seleniumBasicFramework”

2. Create three package as shown below:

objectRepository- It will contain all the static data(object identifier and Application url )
operations– It will contain all frequently used functions used in our testing like- screenshots, external data reading etc.
searchFlight– It will contain the set of code responsible for testing with the help previously defined packages.

Step3 : We will create some folders to save results and to keep our external data source:
a) To add a folder , select project and right click , choose new option and a folder as shown below:

b) We will add three folders as shown below:

Step 4 : Now we will add all the required Jar files(Seelenium webDriver, Apache POI, TestNg Library)
a) TestNg library: Select project->right click-> Java build path->library->add library-> TestNg.
Follow below screenshots:-

b)Selenium WebDriver:- Add selenium WebDriver jar, to download click here and for more detail about it click here

c)Apache POI :- Add POI jar to the project as shown below, for more detail about it click here.

Step 5) Now we will place external drivers and Data source in folders we have mentioned earlier:
ExternalDataSource->excel file having test data.(named as TestData.xlsx)
ExternalDrivers- required external driver for the cross browser testing(chrome driver)

We are done with all basic setup required, let’s create TestNG Class to perform functional testing in package searchFlight:

Step 6 : Select package “searchFlight” ->right click-> new->others->TestNg Class->Next->Finish
a)

b)

c)Provide the name of the class as SearchFlight and click on finish.

d)

Step 7) Add a class in operations name it as “Operations.java”

Step 8) Add methods to Operations class to perform basic functions:

Code Operations.java
package operations;
import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.TakesScreenshot;

public class Operations {
public static void takesScreenshots(WebDriver driver, String name){
try{
TakesScreenshot ts=(TakesScreenshot)driver;
File source=ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source,new File(“./screenshots/”+name+”.png”));
}
catch(Exception e){
System.out.println(“Error while taking Screenshots ->”+e.getMessage());
}}
public static String readExternalData(int i, int j){
String s=null;
try{
File fs = new File(“./ExternalDataSource/TestData.xlsx”);
FileInputStream fis= new FileInputStream(fs);

Workbook workbook= new XSSFWorkbook(fis);
XSSFSheet sheet= (XSSFSheet) workbook.getSheetAt(0);
s=sheet.getRow(i).getCell(j).getStringCellValue();
}
catch(Exception e){
System.out.println(“Error while Reading external data ->”+e.getMessage());
}
return s;
}}

9) Add object identifiers to Util class.

Util.java
package objectRepository;

public class Util {

/////URL
public static final String baseUrl=”https://www.cleartrip.com/”;

// page options:
//public static final String oneay_id=”OneWay”;

public static final String from_xp=”//*[@id=’FromTag’]”;
public static final String to_xp=”//*[@id=’ToTag’]”;
public static final String selectedDate_xp=”//*[@id=’ui-datepicker-div’]/div[1]/table/tbody/tr[4]/td[7]/a”;
public static final String searchbtn_id=”SearchBtn”;
}

10) Add code in SearchFlight.java

SearchFlight.java
package searchFlight;

import java.util.concurrent.TimeUnit;

import objectRepository.Util;
import operations.Operations;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class SearchFlight {

WebDriver driver;// global variable- webdriver reference
@BeforeTest
@Parameters(“browser”)
public void selectBrowser(String browserName){
try{
if(browserName.equalsIgnoreCase(“firefox”)){//takes screenshots with the name passed while function call
DesiredCapabilities dc =new DesiredCapabilities();
dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver= new FirefoxDriver(dc);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.MILLISECONDS);
System.out.println(“Opening firefox with desired capabilities”);
driver.get(Util.baseUrl);
}
else{
System.setProperty(“webdriver.chrome.driver”,”./ExternalDrivers/chromedriver.exe”);
DesiredCapabilities dc= new DesiredCapabilities();
dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver =new ChromeDriver(dc);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.MILLISECONDS);
driver.get(Util.baseUrl);
}}
catch(Exception e){
System.out.println(“unable to launch browser”+e.getMessage());
}}
@Test
public void flightSearch() {// reads data from excel sheet from the specified cell
String from= Operations.readExternalData(1, 0);
String to= Operations.readExternalData(1, 1);
driver.findElement(By.xpath(Util.from_xp)).sendKeys(from);
System.out.println(Operations.readExternalData(1, 0)+” is selected as a source”);
driver.findElement(By.xpath(Util.to_xp)).sendKeys(to);
System.out.println(Operations.readExternalData(1, 1)+” is selected as destination”);
Operations.takesScreenshots(driver,”Selected cities”);
driver.findElement(By.xpath(Util.selectedDate_xp)).click();
driver.findElement(By.id(Util.searchbtn_id)).submit();
}}

Step 11)Create XML file to run the test suite.

As shown screenshot:

Step 12) Once xml is created replace the content with below code:

Steps 13)

Run the test suite by using xml file as shown in below screenshot:

To download complete file click here

Installing Maven in local system

Installing Maven in local system.

In this article we will install Maven, will create dependency and run a simple Selenium test case using  TestNG.

  1. Click here to download Maven jar files from Apache official website.Installing Maven in local system.

2.Unzip the downloaded file and place it in separate folder.

 

Installing Maven in local system.

 

3.Folder Structure

Installing Maven in local system.

 

4. Set Environment variable & path variable.

5.Check Maven version using cmd:- mvn -v

Installing Maven in local system.

6. Now lets create a simple project in eclipse as shown below:

7. Project will be created with folder structure as shown below:

8. Replace Pom.xml content with below provided details:

<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
<modelVersion>4.0.0</modelVersion>
<groupId>automation.maven</groupId>
<artifactId>automation.maven.selenium</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mavenFisrtP</name>

<packaging>jar</packaging>

<properties>
<suiteXmlFile>src/main/resources/testng.xml</suiteXmlFile>

</properties>

<dependencies>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
</dependency>
<!– Adding Selenium dependency –>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>

<suiteXmlFiles>
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>

</configuration>
</plugin>

</plugins>

</build>

</project>

9. Create a simple Selenium test case to open a link in firefox:

Sample code:

 

10. Use mvn clean command to clear all the previous setting.

Installing Maven in local system.

11. Use mvn install command to install missing files as shown in below screenshot:

Installing Maven in local system.

12. Use mvn test command to run the test cases created under maven project:

Installing the M2Eclipse Plugin.

We will complete this in two part – In 1st part we will discuss how to add Maven plug-in

  1. Installing the M2Eclipse Plugin.
  2. Installing Maven in local system.

In this article we will see the 1st part only

Below steps to follow to install Maven on Eclipse IDE :

  1. Open your Eclipse IDE and click Help-> Install New Software…
  2. On the opened pop-up, click on the Addbutton to add a new repository
  3. Fill the form with the information below and press Ok
    Name : M2Eclipse
    Location :http://download.eclipse.org/technology/m2e/releases
Maven Eclipse plugin
Maven Eclipse plugin

4.After the Pending finish, select all the Plugins and press Next >

Maven Eclipse plugin
Maven Eclipse plugin

5.Accept the terms of the license agreement and click Finish

6. At the end of the installation, you will be asked to restart your Eclipse. Click Yes to perform the restart.

Apache Maven is now integrated to your IDE.

 

Checking if Maven has been successfully installed

To check if whether or not Maven has been successfully installed on Eclipse follow these steps:

  1.  Open your Eclipse IDE and click Help -> About Eclipse
  2. Check the presence of the M2Ecplise icon as below
Maven Eclipse plugin
Maven Eclipse plugin