Maven – Config To Run From Command Prompt

1. Download Maven:

http://maven.apache.org/download.cgi

Extract to hard drive (ex: C:\Program Files\apache-maven-3.3.9)

2. Add to System Variables

Open System Variables Table.

  1. Click on Start button
  2. Computer menu item
  3. Properties on right click menu item
  4. Advanced System Settings button on left panel
  5. Advanced tab in System Properties dialog
  6. Environment Variables button
  7. System variables table

windows-7-advanced-setting-option-crunchify-tips

windows-7-environment-variables-crunchify-tips

Add MAVEN_HOME variable

edituservariable

Update PATH variable by add the bin folder of MAVEN_HOME to system path

system-variable

Make sure you have JAVA_HOME variable correctly

javahome

3. Check whether Maven is installed successfully by type command “mvn –version” in cmd

mvnversion

 

TestNG – Run from command line

Formula:

 java -cp "[compile folder]";"[JAR folder]\*" org.testng.TestNG [xml File] 

For example:

java -cp "D:\Automations\Selenium\Workspace\SimpleTest\bin";"D:\Automations\Selenium\Workspace\SimpleTest\JAR File\*" org.testng.TestNG runTest.xml

Or we can create a batch file then edit it like following and save:


D:
cd "D:\Automations\Selenium\Workspace\SimpleTest"
java -cp "D:\Automations\Selenium\Workspace\SimpleTest\bin";"D:\Automations\Selenium\Workspace\SimpleTest\JAR File\*" org.testng.TestNG runTest.xml
pause

Using OOP in Selenium

Idea: Creating a BaseTest class – The class is store all basic method and all others TestClass will be
inherited from it – as shown in below image:

Class diagram

Advantages of using OOP in designing test automation project:

  • Increases code reusability – code to work with one BaseTest class is written only once and used
    in different test cases
  • Improves code maintainability – any change leads to updating the code in BaseTest class only leaving the test classes unaffected
  • Makes code more readable and less brittle

Example:
The BaseTest class:


package myTestPack;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BaseTest {
 protected static WebDriver driver;
 protected static String baseUrl;
 public static void setUp(){
 driver = new FirefoxDriver();
 driver.manage().window().maximize();
 driver.get(baseUrl);
 }
 public void login(String userName, String password) throws InterruptedException {
   driver.findElement(By. cssSelector("#userName")).sendKeys(userName);
   driver.findElement(By. cssSelector("#passWord")).sendKeys(password);
   driver.findElement(By. cssSelector("#submit")).click();
   Thread. sleep(2000);
 }
 public static void tearDown() throws InterruptedException{
   Thread. sleep(2000);
   driver.quit();
 }
 public boolean isElementPresent(By selector) {
   return driver.findElements(selector).size()>0;
 }
 public boolean isElementVisible(By selector){
   return driver.findElement(selector).isDisplayed();
 }
 //Execute java script Code
 public void jsCodeExecution(String jsCode){
   JavascriptExecutor js = (JavascriptExecutor) driver;
   StringBuilder stringBuilder = new StringBuilder();
   stringBuilder.append(jsCode);
   js.executeScript(stringBuilder.toString());
 }
}

The Test Class:


package myTestPack;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
public class TestOne extends BaseTest {
 String textBoxCss = "input.gsfi";
 String searchBtnCss = "span.sbico";
@Test
public void testCase1() throws InterruptedException {
  driver.findElement(By. cssSelector(searchBtnCss)).click();
  Thread. sleep(4000);
  String s = driver.findElement(By. cssSelector( textBoxCss)).getText();
  s = StringUtil. trimSpace(s);
  Assert. assertEquals(s, "selenium");
}
@Test
public void testCase2() throws InterruptedException {
  driver.findElement(By. cssSelector(textBoxCss)).sendKeys("selenium" );
  driver.findElement(By. cssSelector(searchBtnCss)).click();
  Thread. sleep(4000);
}
@BeforeMethod
public void beforeMethod() throws InterruptedException {
  setUp();
  login("userName", "password");
}
@AfterMethod
public void afterMethod() throws InterruptedException {
  tearDown();
 }
}