gpt4 book ai didi

java - 我正在学习 selenium 框架,同时使用测试和 pagefactory 创建框架,我收到以下错误

转载 作者:行者123 更新时间:2023-12-01 17:55:48 25 4
gpt4 key购买 nike

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

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)

最佳答案

问题是,您不在登录/登录页面。您错过了点击主页中的登录链接。请先调用login_link方法,然后再调用login_class类中的login_fun,如下所示。

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"));

关于java - 我正在学习 selenium 框架,同时使用测试和 pagefactory 创建框架,我收到以下错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44970285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com