3

I got an error in my selenium program. I am using page object design pattern. And I want reading test data from properties file. But it couldn't able to read ? Any one please help me to solve page object

package pageobjects;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;


public class HomePage {
    @FindBy(how= How.NAME, using = "username")
    WebElement username;
    @FindBy(how=How.NAME, using = "password")
    WebElement password;
    @FindBy(how=How.XPATH, using="//*[@id=\'login-container\']/form/div[3]/div/p/input[1]" )
    WebElement button;

    //enter  username
    public void  userLogin(String user, String pass)
    {
        username.sendKeys(user);
        password.sendKeys(pass);
        button.click();
    }

}

steps

package steps;


import org.openqa.selenium.support.PageFactory;
import pageobjects.ClientPage;
import pageobjects.HomePage;

import util.DriverManager;


public class LoginSteps {

    public HomePage Login(String nam, String pas) {
        HomePage homePageObj = PageFactory.initElements(DriverManager.driver, HomePage.class);
        homePageObj.userLogin(nam,pas);

        return homePageObj;
    }


}

Util

 package util;

import com.gargoylesoftware.htmlunit.javascript.host.file.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;


/**
 * Created by naveen on 29/11/16.
 */
public class DriverManager {
    public static WebDriver driver;
    String baseUrl;

    public DriverManager() throws IOException {
        // Get baseUrl property
        File file = new File("home/naveen/IdeaProjects/QberTestCase"+"config.properties");
        FileInputStream fileInput = null;
        try {
            fileInput = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //load properties file
        Properties prop = new Properties();
        try {
            prop.load(fileInput);
        } catch (IOException e) {
            e.printStackTrace();
        }




        // Start driver
        System.setProperty("webdriver.chrome.driver", "/home/naveen/chromedriver");
        driver = new ChromeDriver();
        driver.get(baseUrl);
        driver.manage().window().maximize();
    }
}

test

import org.testng.annotations.Test;
import pageobjects.HomePage;
import steps.LoginSteps;
import util.DriverManager;

public class ReadFileData extends DriverManager
{


    @Test
    public void initDriver() {
        //Arrange
        HomePage page = new LoginSteps().Login(prop.getProperty("nam"), prop.getProperty("pass"));

    }


    }

properties file

config.properties
    baseUrl="http://qmsadm"
    nam="naveen"
    pass="789555"
  • Add error which you got after execution of above script. – Bharat Mane Feb 1 '17 at 12:21
  • @BharatMane currently there is no error couldn't read the config.properties file – user21268 Feb 1 '17 at 12:22
  • you may confirm once your property file path. Modify it from starting folder of you driver. like- "D:/home/naveen/IdeaProjects/QberTestCase/config.properties" – Bharat Mane Feb 1 '17 at 12:26
3

You have added wrong code in your "Steps" Class.
Check below sample code -How data is loaded from properties file-

    public void loadPropertiesFile()
    {
        File file = new File(filepath + "object_Repo.properties");
        FileInputStream fileInput = null;
        try {
            fileInput = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //load properties file
        Properties prop = new Properties();
        try {
            prop.load(fileInput);
        } catch (IOException e) {
            e.printStackTrace();
        }

        driver=getDriver();
    }

    public void loginSteps() throws IOException
    {
        homePage obj1= new homePage(driver);
        obj1.userLogin(prop.getProperty("nam"), prop.getProperty("pas"));
        return homePageObj;
    }

}

In your code you missed to write code for loading test data from properties file-

prop.getProperty("nam"), prop.getProperty("pas")

Let me know if it works...

  • 1
    Absolutely right..At the time of loading properties file, we are always using- prop.getProperty(" ") for particular single data, if we want to pass multiple test data add it in a similar way separated with the comma. – Sophia Feb 1 '17 at 12:55
  • @Bharat Mane I have got an error in my code . Can you please check? I edited my code. You can see in the above code – user21268 Feb 2 '17 at 5:27
  • where is your Obj1? You created an object of homepage as 'page' so use it instead of obj1. Also check parameters you used in your properties file- pas instead of pass – Bharat Mane Feb 2 '17 at 5:47
  • will modify your test class plz check updated code.. – Bharat Mane Feb 2 '17 at 6:03
  • 1
    For testing same Login functionality with multiple test data you can use Excel sheet instead of properties file. – Bharat Mane Feb 2 '17 at 8:47
3

Caused by: org.openqa.selenium.WebDriverException: unknown error: unhandled inspector error: {"code":-32603,"message":"Cannot navigate to invalid URL"}

In your DriverManager you have an empty baseurl.

String baseUrl="";
driver.get(baseUrl);

From the Java documentation:

void get(java.lang.String url)

  • Parameters: url - The URL to load. It is best to use a fully qualified URL

An empty string is not a url, not even close :)

In the ReadFileData class you initiate the Driver, but without an url.

Quick fix is to add a real url that can be loaded to the DriverManager.

Maybe you should move the properties loading to the DriverManager? To populate the baseurl. Also maybe ask some developers to help you debug Java errors, you really need to learn and read them.

Example:

public DriverManager() {
    // Get baseUrl property
    File file = new File("config.properties");
    FileInputStream fileInput = new FileInputStream(file);
    Properties prop = new Properties();
    prop.load(fileInput);
    fileInput.close()       
    baseUrl = prop.getProperty('baseUrl');

    // Start driver        
    System.setProperty("webdriver.chrome.driver","/home/naveen/chromedriver");
    driver=new ChromeDriver();
    driver.get(baseUrl);
    driver.manage().window().maximize();
}
  • I edited my post. I got a new error . I want to read the test data from properties file.\ – user21268 Feb 1 '17 at 10:27
  • Now your baseurl is null, leading to this error: Caused by: java.lang.NullPointerException: null value in entry: url=null – Niels van Reijmersdal Feb 1 '17 at 10:30
  • you have to give baseUrl a valid url in your get, so you need to set baseUrl before you do the get somehow. – Niels van Reijmersdal Feb 1 '17 at 10:30
  • I set the baseUrl value in the properties file. – user21268 Feb 1 '17 at 10:32
  • when I put the baseurl value in the DriverManager class file It will load. my intention is to read from properties file. – user21268 Feb 1 '17 at 10:34
0

Try the below code and share your feedback

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExecuteTest {
    static Keywords keyword;
    public static void main(String[] args) throws Exception {
        Keywords key = new Keywords();
        ArrayList a = new ArrayList();
        FileInputStream file = new FileInputStream("C:\\Users\\LENOVO\\eclipse-workspace\\PROJECT2\\src\\objectrepositoryproperty\\ObjectRepository.xlsx");
        XSSFWorkbook workbook  = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheet("OR");
        Iterator row = sheet.iterator();
        while (row.hasNext()) {
            Row rowIterator = (Row) row.next();
            Iterator cellItr = rowIterator.cellIterator();
            while (cellItr.hasNext()) {
                Cell cell = (Cell) cellItr.next();
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    a.add(cell.getStringCellValue());
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    a.add(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    a.add(cell.getNumericCellValue());
                    break;
                case Cell.CELL_TYPE_BLANK:
                    a.add(cell.getDateCellValue());
                    break;
                }
            }
        }

              for (int i=0;i<a.size();i++){

            if (a.get(i).equals("openbrowser")) {

                String keyword =(String)a.get(i);
                String data =(String)a.get(i+1);
                String objectname =(String)a.get(i+2);
                String runmode =(String)a.get(i+3);
                System.out.println(keyword);
                System.out.println(data);
                System.out.println(objectname);
                System.out.println(runmode);
                if(runmode.equals("yes")) {
                    key.openbrowser();

                    }
            }
            if (a.get(i).equals("navigate")) {
                String keyword =(String)a.get(i);
                String data =(String)a.get(i+1);
                String objectname =(String)a.get(i+2);
                String runmode =(String)a.get(i+3);
                System.out.println(keyword);
                System.out.println(data);
                System.out.println(objectname);
                System.out.println(runmode);
                if(runmode.equals("yes")) {
                    key.navigate(data);
                }

            }



            }


    }
}

keyword.java

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Keywords {
           static FirefoxDriver driver;


    public void openbrowser() {
          System.setProperty("webdriver.gecko.driver","C:\\geckodriver");

           driver = new FirefoxDriver();

           driver.manage().timeouts().implicitlyWait(1,TimeUnit.HOURS);


    }
    public void navigate (String data) {
           driver.get("www.google.com");
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy