Maven

Installation of Maven:
  • Open Maven URL link
  • To download Maven Click on the link apache-maven-x.x.x-bin.zip on current page
  • Extract the zip file
  • Create a new system environment variable
  • Name: M2_HOME
  • Value: parentPath\apache-maven-x.x.x-bin\apache-maven-x.x.x
  • Edit the existing Path system environment variable
  • Name: Path
  • Value: parentPath\apache-maven-x.x.x-bin\apache-maven-x.x.x\bin
Create a Maven Project using Command prompt:
  • To verify maven is installed or not?
  • mvn --version 
  • To create a maven project
  • Create a folder where your project will be present
  • mkdir MavenProject
  • cd MavenProject
  • mvn archetype:generate
  • Choose a number (type of project) or simply press enter key it will choose default project type
  • Choose a quickstart version
  • Give a groupId e.g. com.maven.test
  • Give an artifactId e.g. MavenDemo
  • Enter package name
  • Accept confirmation
  • Go to the MavenProject folder and verify that Maven project is created or not?
  • Convert the maven project to eclipse understandable
  • cd MavenDemo
  • mvn eclipse:eclipse
  • Import the maven project into eclipse File -> Import -> General -> Import Existing Project
  • Maven dependencies for pom.xml
  • <dependencies>
    <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.0.1</version>
    </dependency>
    <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.8</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>4.0</version>
    </dependency>
    <dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6</version>
    </dependency>
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.14</version>
    </dependency>
    <dependency>
    <groupId>org.uncommons</groupId>
    <artifactId>reportng</artifactId>
    <version>1.1.4</version>
    </dependency>
    <dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-tools</artifactId>
    <version>2.0</version>
    </dependency>
    </dependencies>

  • Run testing.xml from pom.xml add plugins
  • <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
    <suiteXmlFiles>
    <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
    </suiteXmlFiles>
    </configuration>
    </plugin>
    </plugins>
    </build>
  • Reportng listeners in testng.xml
  • <listeners>
           <listener class-name="org.uncommons.reportng.HTMLReporter"/>
           <listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
           <listener class-name="com.test.CustomListener"/>
    </listeners>

FacebookLoginTest.java
package com.facebook.login;

import org.testng.annotations.Test;
import java.io.FileInputStream;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import jxl.Sheet;
import jxl.Workbook;
import utility.Helper;

public class FacebookLoginTest {
    WebDriver driver;

    public List getElements(String locatorName, String locatorValue) {
        List elements = null;
        switch (locatorName) {
        case "name":
            elements = driver.findElements(By.name(locatorValue));
            break;
        case "linkText":
            elements = driver.findElements(By.linkText(locatorValue));
            break;
        case "partialLinkText":
            elements = driver.findElements(By.partialLinkText(locatorValue));
            break;
        case "className":
            elements = driver.findElements(By.className(locatorValue));
            break;
        case "xpath":
            elements = driver.findElements(By.xpath(locatorValue));
            break;
        case "css":
            elements = driver.findElements(By.cssSelector(locatorValue));
            break;
        case "tagName":
            elements = driver.findElements(By.tagName(locatorValue));
            break;
        }
        return elements;
    }

    public WebElement getElement(String locatorName, String locatorValue) {
        WebElement element = null;
        switch (locatorName) {
        case "id":
            element = driver.findElement(By.id(locatorValue));
            break;
        case "name":
            element = driver.findElement(By.name(locatorValue));
            break;
        case "linkText":
            element = driver.findElement(By.linkText(locatorValue));
            break;
        case "partialLinkText":
            element = driver.findElement(By.partialLinkText(locatorValue));
            break;
        case "className":
            element = driver.findElement(By.className(locatorValue));
            break;
        case "xpath":
            element = driver.findElement(By.xpath(locatorValue));
            break;
        case "css":
            element = driver.findElement(By.cssSelector(locatorValue));
            break;
        case "tagName":
            element = driver.findElement(By.tagName(locatorValue));
            break;
        }
        return element;
    }

    public void performTask(WebElement element, String type, String... text) {
        switch (type) {
        case "text":
            element.sendKeys(text[0]);
            break;
        case "select":
            Select select = new Select(element);
            select.selectByVisibleText(text[0]);
            break;
        case "button":
            element.click();
            break;
        case "link":
            element.click();
            break;
        case "checkbox":
            element.click();
            break;
        case "radiobutton":
            element.click();
            break;
        }
    }

    @BeforeMethod
    public void beforeLoginTest(){
        driver = Helper.launchBrowser("chrome");
        driver.get("http://facebook.com");
    }
    
    @Test(dataProvider = "testData")
    public void facebookLogin(String userID, String pass) throws Exception {
        System.out.println(userID + "\t" + pass);
        FileInputStream fis = new FileInputStream("src/test/resources/Facebook_Login.xls");
        Workbook wb = Workbook.getWorkbook(fis);
        Sheet sht = wb.getSheet("Locators");

        String locatorName, locatorValue;
        locatorName = sht.getCell(0, 0).getContents();
        locatorValue = sht.getCell(0, 1).getContents();
        performTask(getElement(locatorName, locatorValue), "text", userID);
        
        locatorName = sht.getCell(1, 0).getContents();
        locatorValue = sht.getCell(1, 1).getContents();
        performTask(getElement(locatorName, locatorValue), "text", pass);
    
        locatorName = sht.getCell(2, 0).getContents();
        locatorValue = sht.getCell(2, 1).getContents();
        performTask(getElement(locatorName, locatorValue), "button");
    }

    @Test
    @DataProvider
    public Object[][] testData() throws Exception {
        Object[][] data = null;
        FileInputStream fis = new FileInputStream("src/test/resources/Facebook_Login.xls");
        Workbook wb = Workbook.getWorkbook(fis);
        Sheet sht = wb.getSheet("Login Data");

        int rows = sht.getRows(),
              col = sht.getColumns();
        System.out.println("No of Rows: " + rows + "\tNo of Columns: " + col);

        data = new Object[rows-1][col];

        for (int i = 0; i < rows - 1; i++) {
            for (int j = 0; j < col; j++) {
                data[i][j] = sht.getCell(j, i + 1).getContents();
                System.out.print(data[i][j] + " ");
            }
            System.out.println();
        }
        return data;
    }
    
    @AfterMethod
    public void afterLogin(){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        driver.close();
    }
}

FacebookRegistrationTest.java
package com.facebook.registration;

import org.testng.annotations.Test;
import java.io.FileInputStream;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import jxl.Sheet;
import jxl.Workbook;
import utility.Helper;

public class FacebookRegistrationTest {
    WebDriver driver;

    public List getElements(String locatorName, String locatorValue) {
        List elements = null;
        switch (locatorName) {
        case "name":
            elements = driver.findElements(By.name(locatorValue));
            break;
        case "linkText":
            elements = driver.findElements(By.linkText(locatorValue));
            break;
        case "partialLinkText":
            elements = driver.findElements(By.partialLinkText(locatorValue));
            break;
        case "className":
            elements = driver.findElements(By.className(locatorValue));
            break;
        case "xpath":
            elements = driver.findElements(By.xpath(locatorValue));
            break;
        case "css":
            elements = driver.findElements(By.cssSelector(locatorValue));
            break;
        case "tagName":
            elements = driver.findElements(By.tagName(locatorValue));
            break;
        }
        return elements;
    }

    public WebElement getElement(String locatorName, String locatorValue) {
        WebElement element = null;
        switch (locatorName) {
        case "id":
            element = driver.findElement(By.id(locatorValue));
            break;
        case "name":
            element = driver.findElement(By.name(locatorValue));
            break;
        case "linkText":
            element = driver.findElement(By.linkText(locatorValue));
            break;
        case "partialLinkText":
            element = driver.findElement(By.partialLinkText(locatorValue));
            break;
        case "className":
            element = driver.findElement(By.className(locatorValue));
            break;
        case "xpath":
            element = driver.findElement(By.xpath(locatorValue));
            break;
        case "css":
            element = driver.findElement(By.cssSelector(locatorValue));
            break;
        case "tagName":
            element = driver.findElement(By.tagName(locatorValue));
            break;
        case "text":
            element = driver.findElement(By.xpath("//*[text()='" + locatorValue + "']"));
            break;
        }
        return element;
    }

    public void performTask(WebElement element, String type, String... text) {
        switch (type) {
        case "text":
            element.click();
            element.clear();
            element.sendKeys(text[0]);
            break;
        case "select":
            Select select = new Select(element);
            select.selectByVisibleText(text[0]);
            break;
        case "button":
            element.click();
            break;
        case "link":
            element.click();
            break;
        case "checkbox":
            element.click();
            break;
        case "radiobutton":
            element.click();
            break;
        }
    }

    @BeforeMethod
    public void beforeRegistrationTest(){
        driver = Helper.launchBrowser("ff");
        driver.get("http://facebook.com");
    }

    @Test(dataProvider = "regData")
    public void facebookRegistration(String fn, String sn, String email,
        String re_enter, String pass, String day, String month,
        String year, String gender) throws Exception {
        
        String locatorName, locatorValue;
        WebElement element;
        FileInputStream fis = new FileInputStream("src/test/resources/Facebook_Registration.xls");
        Workbook wb = Workbook.getWorkbook(fis);
        Sheet sht = wb.getSheet("Locators");

        locatorName = sht.getCell(0, 0).getContents();
        locatorValue = sht.getCell(0, 1).getContents();
        element = getElement(locatorName, locatorValue);
        performTask(element, "text", fn);
        
        locatorName = sht.getCell(1, 0).getContents();
        locatorValue = sht.getCell(1, 1).getContents();
        element = getElement(locatorName, locatorValue);
        performTask(element, "text", sn);
    
        locatorName = sht.getCell(2, 0).getContents();
        locatorValue = sht.getCell(2, 1).getContents();
        element = getElement(locatorName, locatorValue);
        performTask(element, "text", email);
        
        locatorName = sht.getCell(3, 0).getContents();
        locatorValue = sht.getCell(3, 1).getContents();
        element = getElement(locatorName, locatorValue);
        performTask(element, "text", re_enter);
        
        locatorName = sht.getCell(4, 0).getContents();
        locatorValue = sht.getCell(4, 1).getContents();
        element = getElement(locatorName, locatorValue);
        performTask(element, "text", pass);
        
        locatorName = sht.getCell(5, 0).getContents();
        locatorValue = sht.getCell(5, 1).getContents();
        element = getElement(locatorName, locatorValue);
        performTask(element, "select", day);
        
        locatorName = sht.getCell(6, 0).getContents();
        locatorValue = sht.getCell(6, 1).getContents();
        element = getElement(locatorName, locatorValue);
        performTask(element, "select", month);
        
        locatorName = sht.getCell(7, 0).getContents();
        locatorValue = sht.getCell(7, 1).getContents();
        element = getElement(locatorName, locatorValue);
        performTask(element, "select", year);
        
        element = getElement("text", gender);
        performTask(element, "button");
    }

    @AfterMethod
    public void afterRegistrationTest() throws InterruptedException{
        Thread.sleep(5000);
        driver.close();
    }
    @Test
    @DataProvider(name = "regData")
    public Object[][] testData() throws Exception {
        Object[][] data = null;
        FileInputStream fis = new FileInputStream("src/test/resources/Facebook_Registration.xls");
        Workbook wb = Workbook.getWorkbook(fis);
        Sheet sht = wb.getSheet("Registration");

        int rows = sht.getRows(),
              col = sht.getColumns();
        System.out.println("No of Rows: " + rows + "\tNo of Columns: " + col);

        data = new Object[rows-1][col];

        for (int i = 0; i < rows - 1; i++) {
            for (int j = 0; j < col; j++) {
                data[i][j] = sht.getCell(j, i + 1).getContents();
                System.out.print(data[i][j] + " ");
            }
            System.out.println();
        }
        return data;
    }
}

No comments:

Post a Comment