Home page

Wednesday, September 25, 2019

Data driven testing on Facebook using Selenium + JAVA

  1. Create maven project
  2. Convert project to TestNG
  3. Create pageobject, Utility and facebooktest packages
  4. Create Driver and TestData folders.
  5. Copy following code to facebooktest > AppTest.java file
  6. package com.wallethub.faceboook;

    import java.io.FileInputStream;
    import java.io.IOException;

    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.DataFormatter;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;

    import pageobject.HomePageObject;
    import pageobject.LoginPageObject;
    import utilities.BaseClass;

    public class AppTest extends BaseClass{
       
        public static String file_location = System.getProperty("user.dir")+"//TestData//Login.xls";
        public static HSSFWorkbook workbook;
        public static HSSFSheet worksheet;
        static String SheetName= "Sheet1";
        public static DataFormatter formatter= new DataFormatter();
       
        LoginPageObject LPO = new LoginPageObject();
        HomePageObject HPO = new HomePageObject();
       
        @Test(dataProvider="readTestData")
        public void testApp(String UserName, String Password) throws InterruptedException{
            LPO.login(UserName, Password);
            HPO.post();
        }
       
        @DataProvider(name="readTestData")
        public static Object[][] ReadVariant() throws IOException
        {
        FileInputStream fileInputStream= new FileInputStream(file_location);
            workbook = new HSSFWorkbook (fileInputStream);
            worksheet=workbook.getSheet(SheetName);
            HSSFRow Row=worksheet.getRow(0);       
        
            int RowNum = worksheet.getPhysicalNumberOfRows();
            int ColNum= Row.getLastCellNum(); 
            
            Object Data[][]= new Object[RowNum-1][ColNum];
            
                for(int i=0; i<RowNum-1; i++)
                { 
                    HSSFRow row= worksheet.getRow(i+1);
                    
                    for (int j=0; j<ColNum; j++)
                    {
                        if(row==null)
                            Data[i][j]= "";
                        else
                        {
                            HSSFCell cell= row.getCell(j);
                            if(cell==null)
                                Data[i][j]= ""; 
                            else
                            {
                                String value=formatter.formatCellValue(cell);
                                Data[i][j]=value;
                            }
                        }
                    }
                }

            return Data;
        }
    }
  7. Copy following code into pageobject > LoginPageObject.java file
  8. package pageobject;

    import org.openqa.selenium.By;
    import org.openqa.selenium.support.PageFactory;

    import utilities.BaseClass;

    public class LoginPageObject extends BaseClass{
       
        public LoginPageObject() {
            PageFactory.initElements(driver, this);
        }
       
        By username = By.id("email");
        By password = By.id("pass");
        By loginBTN = By.xpath("//input[contains(@value,'Log In')]");
        public void login(String UserName, String Password){
            driver.findElement(username).sendKeys(UserName);
            driver.findElement(password).sendKeys(Password);
            driver.findElement(loginBTN).click();
        }
    }
  9. Copy following code into Utility > BaseClass.java file
  10. package utilities;

    import java.util.HashMap;
    import java.util.Map;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Optional;
    import org.testng.annotations.Parameters;

    public class BaseClass {
       
        protected static WebDriver driver;
        Map<String, Object> prefs = new HashMap<String, Object>();
        ChromeOptions options = new ChromeOptions();
       
        @Parameters("browser")
        @BeforeTest(alwaysRun=true)
        public void setupWebdriver(@Optional ("chrome") String browser){
            browserLaunch(browser);
        }

        public void browserLaunch(String browser){
           
            prefs.put("profile.default_content_setting_values.notifications", 2);
            if(browser.equals("chrome")){
                options.setExperimentalOption("prefs", prefs);
                System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"//drivers//chromedriver.exe");
                driver = new ChromeDriver(options);
                driver.manage().window().maximize();
            }
           
            driver.get("https://www.facebook.com/");
        }
    }
  11. Copy following code into Utility > Tools.java file
  12. package utilities;

    import org.openqa.selenium.By;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class Tools extends BaseClass {
       
        static WebDriverWait wait;
       
        public static void waitForElementToBePresent(By by, int timeOutSeconds) {
            wait = new WebDriverWait(driver, timeOutSeconds);
            wait.until(ExpectedConditions.presenceOfElementLocated(by));
        }

    }
  13. Create Login.xls file under TestData folder with credentials
  14. Copy following dependencies in POM.xml file
  15. <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>3.141.59</version>
            </dependency>

            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.14.3</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.uncommons</groupId>
                <artifactId>reportng</artifactId>
                <version>1.1.4</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>4.1.0</version>
            </dependency>

            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>4.1.0</version>
            </dependency>
        </dependencies>
  16. Copy following code into TestNG.xml file
  17. <suite name="Suite">
      <test thread-count="5" name="Test">
      <parameter name="browser" value="chrome"/>
        <classes>
          <class name="faceboooktest.AppTest"/>
        </classes>
      </test> <!-- Test -->
    </suite> <!-- Suite -->

No comments:

Post a Comment