Selenium WebDriver – Package the script into Jar

Creating Jar File:

In my previous post, i discussed how to create Bat file to run the script from Command prompt using Java commands.

Another popular way (as mentioned in that post) is by packaging the script (java program) into Jar and running the jar from bat file. If you are working on CI/CD project, this will help you to run the script by triggering the bat file from Jenkins job.

 

From your eclipse IDE, you can select File –> Export, Select Runnable Jar File

Capture

This is important step, here you need to mention the package name in “Launch configuration:” where the Java Main Class is located. This will be used by eclipse to update Manifest file accordingly.

 

Capture

 

Once done, you will see the Jar file in the location you selected above.

Creating the batch file:

Make sure your jar is inside project root.

Open a notepad, write the below details (by updating the location where your project folder is present) and save it with “.bat” extension

</pre>
cd  C:\Selenium\

java -jar Test.jar
<pre>

 

Running Selenium WebDriver script from bat file without packaging

If you wanna trigger your selenium script automatically using any kind of trigger mechanism (Windows Scheduler, Bot, CI Tools etc..) then, there are 2 popular options

  1. Package your script into jar and trigger it through Bat file
  2. Create a batch file to run the script from command prompt. Get this batch file triggered through task scheduler in windows

I will discuss the later option here.

In order to trigger the script from command prompt, you need to do the following:

  1. change the directory to the one where your main class file is located
  2. Provide the path of all jars that you use in your script, along with full path of class file that contains main function

Below is the example:

CD "C:\Selenium\WorkspaceForTestDataAutomation\TestDataAutomation-SAFT\SAFT-TestDataAutomation - itqa-framework\SAFT\"

java -cp ".;./supportlibraries/External_Jars/*;./supportlibraries/DB/*;./supportlibraries/*;./supportlibraries/External_Jars/commons-codec-1.10/*;./supportlibraries/External_Jars/extentreports-java-2.41.2/*;./supportlibraries/External_Jars/extentreports-java-2.41.2/lib/*;" allocator/Allocator

pause

 

Selenium WebDriver Script to clear browser history in Chrome

There will be some applications that requires the browser history to be cleaned before launching it. The below function helps to do the same in Java

Notice that you will have to give a large wait time since you may not know how much of data is there in cache and how much time your system may take to clear it out.

 public void clearChromeBrowsingHistory(){
//set the implicit wait time to 60 secs
sync.setImplicitWait(2);
driver.get("chrome://settings/clearBrowserData");
WebElement clearDataBtn = driver.findElement(By.cssSelector("* /deep/ #clearBrowsingDataConfirm"));
if(sync.isElementPresent(By.cssSelector("* /deep/ #clearBrowsingDataConfirm"))){
System.out.println("Clear button found");
clearDataBtn.click();
}
//int wait=0;
do{
//System.out.println("Waiting for cache to clear:"+wait);
//wait++;
}while(sync.isElementDisplayed(By.cssSelector("* /deep/ #clearBrowsingDataConfirm")));
//set the implicit wait time back to 30 secs
sync.setImplicitWait(30);
}