Run selenium testng tests from executable jar



How to run Selenium testng tests from Executable jar file.?


Have you ever thought of to run some set of tests and convert those test to executable jar and share that jar file to team members who are non -technical / business members so that with simple double clicks those test would run.? If yes, here is how to achieve the it.

pre-requisite:
 > Have a directory with all required libraries, say directory name as 'lib', then put the following jar files, 
a) chromedriver
b) jcommander-1.78.jar (latest version jar)
c) selenium-server-standalone
d) testng-6.8.8.jar (latest version compatible jar) 

1) A simple selenium testng test implemented in eclipse. (make sure test is running)
a) Create a java file with main method, for ex:
public class TestRunner {
static TestNG testng;
public static void main(String[] args) {
testng = new TestNG();
testng.setTestClasses(new Class[] {fayaz.google.searchtest.GoogleSearch.class});
testng.run();
}
}
b) From this java file, run the tests as java application to make sure tests are running
2) Right click on the project and click 'Export...'
3) Under 'Java' select 'Runnable JAR file'
4) Click Next, select the Launh file as, 'TestRunner' (java file name) and export to desktop
5) Open command prompt from the project directory ( NOT FROM desktop)
6) Now, double click the executable jar by providing the absolute path, with java -jar command, 
         for ex: D:\fayaz\workspace\TestRunnerDemo\> java -jar C:\Users\Fayaz\Desktop\testrunner.jar

Note: if the jar is running from the desktop directory then test doesn't run. 


=============================
Now, to make run the jar from any directory we need to have the chrome driver accessible.
To make chrome driver accessible, we need to set the driver path in environment variable, after which we need to restart the eclipse..  

Then set the chrome driver path as below..

String evn_var_chrome_driver_path = System.getenv("chrome_driver_path");
System.setProperty("webdriver.chrome.driver", evn_var_chrome_driver_path + "\\chromedriver");

Boommm.. now convert the project to executable java jar file and run it from any directory





Quick Reference - Selenium Interview Questions

* What is hard asserstion and soft assertion

* Handle alerts  -->  driver.switchTo().alert().accept();

* Select options

Select select = new Select(webElement); //select.getOptions();
select.selectByIndex(1);

* tesng.xml example
   <suite name="TestSuiteName" parallel="tests">
     <test name="TestName" preserve-order="true">
              <classes>
<class name="org.pacakge.name.TestClassName" />
     </classes>
     </test>
   </suite>

* how to disable web security in chrome
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-web-security");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

* how to take screenshot
TakesScreenshoot screenshot = ((TakesScreenshot)driver)
File sourceFile = screenshot.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, "filename.jpg");

* Difference between navigate().to(URL) and driver.get(URL)

* Difference between implicitWait and explicitWait

* Wait for pageLoad
WebDriverWait wait = new WebDriverWait(driver, timeoutInSecs);
ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>(){
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript(
"return document.readyState").equals("Complete");
}
};
wait.until(pageLoadCondition);


* KeyboardActions
Ex: Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();

* Difference between testNG and JUnit

* assertions and types

* Run TestNG using cucumber
use following classes
MultiLoader()
RuntimeOptionsFactory()
PluginFactory()
ResourceLoaderClassFinder()

* ErrorCollector in JUnit

* Javascript in selenium

Ex: String MAX_BROWSER_WIN = "if (window.screen){window.moveTo(0,0); window.resizeTo(window.screen.availWidth, window.screen.availHeight);};";
((JavascriptExecutor)driver).executeScript(MAX_BROWSER_WIN);

Ex: ((JavascriptExecutor)driver).executeScript("scroll(250, 0)");

* configuration of testng in maven
<pluings>
<plugin>
-----
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>

* configuration of reports in maven

* grouping from testng.xml

<suite>
<test name="testName">
<groups>
<define name="includeGroup">
<inlucde name="includeTestOne" />
<include name="includeTestTwo" />
</define>
<define name="exlcudeGroup">
<exclude name="excludeTestOne" />
<exclude name="excludeTestTwo" />
</define>
<run>
<include name="include-group" />
<exclude name="exclude-group" />
</run>
</groups>
</test>
</suite>

* customization of test execution reports
using XSLT reports using testng+ANT

* Design patterns in selenium
DomainDrivenDesign: Express your tests in the language of the end-user of the app.
PageObjects  : A simple abstraction of the UI of your web app.
LoadableComponent : Modeling PageObjects as components.
BotStyleTests  : Using a command-based approach to automating tests, rather than the object-based approach that PageObjects encourage
AcceptanceTests   : Use coarse-grained UI tests to help structure development work.
RegressionTests   : Collect the actions of multiple AcceptanceTests into one place for ease of maintenance.

Capture network calls and performance stats





Capturing Network calls:

To capture the network calls while loading a page, we need to use java script executor, ]the following example may helps:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("disable-info");
    options.addArguments("start-maximized");
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    driver = new ChromeDriver(capabilities);
    driver.get(url);
    //switch to Browser  Functions tab for code implementation
    String netData = ((JavascriptExecutor) driver).executeScript(scriptToExecute).toString();
    BrowserFunctions.waitUntilPageLoads(driver);
    String scriptToExecute = "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {};
return network;";
    System.out.println(netData);

Note: The out put in json format.



Capture Network performance:

DesiredCapabilities d = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
d.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
WebDriver driver = new ChromeDriver(d);
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
LogEntries les = driver.manage().logs().get(LogType.PERFORMANCE);
for (LogEntry le : les) {
    System.out.println(le.getMessage());
}