我正在学习selenium框架,同时创建框架与测试和pagefactory我得到跟随错误

时间:2017-07-07 12:01:27

标签: java selenium selenium-webdriver frameworks testng

我得到的文件和错误如下:

url_title.properties

username=rakeshm@techved.com   
password=123456   
chrome_driver_path=D://rakesh//software//selenium browser//chrome 2.28//chromedriver.exe    

BrowserFactory类文件

package utilities;   
import java.io.File;    
import java.io.FileInputStream;    
import java.io.IOException;   
import java.util.Properties;   
import org.openqa.selenium.WebDriver;    
import org.openqa.selenium.chrome.ChromeDriver;  
import org.openqa.selenium.firefox.FirefoxDriver;  
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class BrowserFactory   
{  
    static Properties browserfactory_properties_obj;  
    static WebDriver driver;  
public static WebDriver browser_factory_getter(String browser, String url) throws IOException  
{  
    File browserfactory_file_obj = new  File(".\\src\\property_folder\\url_title.properties");   
    FileInputStream browserfactory_fileinput_obj = new FileInputStream(browserfactory_file_obj);            
    browserfactory_properties_obj = new Properties();   
    browserfactory_properties_obj.load(browserfactory_fileinput_obj);           
    if(browser.equalsIgnoreCase("firefox"))     
    {    
        ProfilesIni profile = new ProfilesIni();    
        FirefoxProfile fire_profile = profile.getProfile("selenium_browser");    
        driver = new  FirefoxDriver(fire_profile);    
    }   
    else if(browser.equalsIgnoreCase("chrome"))   
    {  
        System.setProperty("webdriver.chrome.driver",browserfactory_properties_obj.getProperty("chrome_driver_path"));    
        driver = new ChromeDriver();    
    }               
    driver.manage().window().maximize();     
    driver.get(url);     
    return driver;     
}    
}    

home_login_elements类文件

package utilities;    
import org.openqa.selenium.WebDriver;    
import org.openqa.selenium.WebElement;    
import org.openqa.selenium.support.FindBy;    
import org.openqa.selenium.support.How;    

public class home_login_elements    
{    
    WebDriver driver;    
    @FindBy(how=How.CLASS_NAME, using="login")   
    WebElement login_link_ele;    

    @FindBy(how=How.ID, using="email")   
    WebElement username_ele;   

    @FindBy(how=How.ID, using="passwd")   
    WebElement password_ele;   

    @FindBy(how=How.ID, using="SubmitLogin")   
    WebElement login_submint_ele;   

public WebElement login_link()   
{   
    login_link_ele.click();   
    return login_link_ele;   
}   

public void login_fun(String username, String password)   
{   
    username_ele.sendKeys(username);   
    password_ele.sendKeys(password);   
    login_link_ele.submit();   
}   
}   

login_class类文件

package utilities;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;

public class login_class 
{
WebDriver driver;
Properties login_class_url_title;   
public login_class() throws Exception
{
    File login_class_url_title_file = new File(".\\src\\property_folder\\url_title.properties");
    FileInputStream login_class_url_title_fis = new FileInputStream(login_class_url_title_file);
    login_class_url_title = new Properties();
    login_class_url_title.load(login_class_url_title_fis);
    home_login_elements home_login_elements_login_class_obj = PageFactory.initElements(driver, home_login_elements.class);
    home_login_elements_login_class_obj.login_fun(login_class_url_title.getProperty("username"), login_class_url_title.getProperty("password"));
}
}

Add_to_wishlist testng文件(可执行文件)

package wishlist;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import utilities.BrowserFactory;
import utilities.home_login_elements;
import utilities.login_class;

public class Add_to_wishlist extends home_login_elements
{       
WebDriver driver;
@Test
public void first_testcase() throws Exception
{
    driver = BrowserFactory.browser_factory_getter("chrome", "http://automationpractice.com/index.php");
    login_class obj =new login_class();
    driver.findElement(By.xpath("//ul[@class='sf-menu clearfix menu-content sf-js-enabled sf-arrows']/li[3]")).click();     
}
}

执行Add_to_wishlist testng文件后,我收到以下错误

Starting ChromeDriver 2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41) on port 6905
Only local connections are allowed.
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
FAILED: first_testcase
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy5.sendKeys(Unknown Source)
at utilities.home_login_elements.login_fun(home_login_elements.java:36)
at utilities.login_class.<init>(login_class.java:20)
at wishlist.Add_to_wishlist.first_testcase(Add_to_wishlist.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

3 个答案:

答案 0 :(得分:2)

问题是,您没有登录/登录页面。你错过了点击主页上的登录链接。请在调用login_class类中的login_fun之前调用login_link方法,如下所示。

driver=BrowserFactory.driver;
home_login_elements home_login_elements_login_class_obj = PageFactory.initElements(driver, home_login_elements.class);
home_login_elements_login_class_obj.login_link();//This is missing in your code
home_login_elements_login_class_obj.login_fun(login_class_url_title.getProperty("username"), login_class_url_title.getProperty("password"));

答案 1 :(得分:1)

我没有在课堂上看到你初始化你的元素。

public class home_login_elements {
    public home_login_elements(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }
}

答案 2 :(得分:1)

您的login_class似乎不知道您已初始化的driver,因此它是NULL

在您的情况下,请进行以下更改:

login_class中更改构造函数以获取驱动程序:

public login_class(WebDriver webDriver) throws Exception
{
    driver = webDriver;
    ...
}

first_testcase()

{
    ...
    login_class obj =new login_class(driver);
    ...
}

有人说......这是一个奇怪的实现,我建议再看一些其他人如何在最初设置这个设置的例子。但是,如果它适合你,那就太棒了:))

对于它的价值,我认为@murthi提供的答案足以正确设置driver,并且可能应该是可接受的答案,因为这是做你要求的最简单的方法。 / p>