我正在创建页面对象模型和数据驱动程序框架。我正在为登录编写测试用例,但得到了pagefactory nullpointerexception。1.如何初始化我的驱动程序以避免此错误?2.再次说明如何在我的测试脚本中使用Screenshot page类,我在下面给出了代码。
失败:日志(“s@gmail.com“,”sw45“)java.lang.NullPointerException位于org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
TestBase类
公共类TestBase{public WebDriver;
public void initialize() throws InterruptedException, IOException {
System.out.println("Launching browser");
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\admin\\eclipse-workspace\\SampleProject\\src\\main\\java\\selenium\\org\\sample\\SampleProject\\data\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.navigate().to("http://automationpractice.com/index.php?controller=authentication&back=my-account");
Thread.sleep(6000);
}
}
AppTest java
公共类AppTest扩展ExcelReader{TestBase TB=new TestBase();
@BeforeTest
void browserlaunch() throws InterruptedException, IOException
{
TB.initialize();
}
@Test(dataProvider = "testdata")
public void LogIn(String email, String pwd) throws IOException, InterruptedException {
System.out.println("Sign in page1");
SignIn loginpage = PageFactory.initElements(driver, SignIn.class);
loginpage.setUserName(email);// email entered
loginpage.setPwd(pwd);// password entered
loginpage.Sign_In_btn();
driver.manage().window().maximize();
try {
Assert.assertEquals(driver.getTitle(), "My account - My Store");
System.out.println("Log IN successfull1");
} catch (AssertionError E) {
System.out.println("Log IN un-successfull" + E);
}
Thread.sleep(8000);
System.out.println("after click");
}
}
截屏页面java
public class ScreenshotPage扩展了TestBase{private WebDriver driver=new ChromeDriver();
public void ScreenshotPage1() throws InterruptedException, IOException {
Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000))
.takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(), "PNG", new File("D:/selenium/" + System.currentTimeMillis() + ".png"));
}
}
package com.xyz33;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class DemoPOMclass {
//static WebDriver driver;
static String url1="https://www.facebook.com/";
static WebDriver driver;
public DemoPOMclass(WebDriver driver) {
this.driver=driver; PageFactory.initElements(driver, this);// initElements() method will create all WebElements
}
@FindBy(name="email")
WebElement userID;
@FindBy(id="pass")
WebElement passwrd;
@FindBy(id="u_0_d_Ek")
WebElement btn_login;
public WebDriver launchBrowser() {//just open chrome browser using automation
System.setProperty("webdriver.chrome.driver" ,"C:/Users/tgt193/Desktop/traningAll/AllDrivers/chromedriver.exe" );
System.out.println("browser is launch successfully");
driver=new ChromeDriver();//type casting
driver.manage().window().maximize(); //maximizes the open browser
System.out.println("browser maximize sussfully");
return driver;
}
public WebDriver openUrl(String url1) {//open URL using automation
driver.get(url1);
System.out.println("URL is launch successfully");
return driver;
}
public void enterUserName(String uName){
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
System.out.println("wait 3 sec ");
System.out.println("enter username at FB page");
userID.sendKeys(uName);
System.out.println("username enter successfully at FB page");
}
public void enterPassword(String uPasswd){
passwrd.sendKeys(uPasswd);
System.out.println("enter password at FB page");
}
public void click_onLogInButton(){
btn_login.click();
}
public void login_ToFB() {
this.launchBrowser();
this.openUrl("https://www.facebook.com/");
this.enterUserName("sunit");
this.enterPassword("sunit123@");
this.click_onLogInButton();
}
public static void main(String[] args) {
DemoPOMclass objPOM =new DemoPOMclass(driver);
objPOM.launchBrowser();
objPOM.openUrl(url1);
objPOM.enterUserName("sunit");
objPOM.enterPassword("sunit123");
//objPOM.login_ToFB();
//objPOM.login_ToFB();
}
}
使此方法返回WebDriver:
public WebDriver initialize() throws InterruptedException, IOException {
System.out.println("Launching browser");
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\admin\\eclipse-workspace\\SampleProject\\src\\main\\java\\selenium\\org\\sample\\SampleProject\\data\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.navigate().to("http://automationpractice.com/index.php?controller=authentication&back=my-account");
Thread.sleep(6000);
return driver;
}
在AppTest类中,将其用作:
public class AppTest extends ExcelReader {
public WebDriver driver;
TestBase TB = new TestBase();
@BeforeTest
void browserlaunch() throws InterruptedException, IOException
{
driver = TB.initialize();
}
对于屏幕截图,将您的方法设置为:
public static void ScreenshotPage1(WebDriver driver) throws InterruptedException, IOException {
Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000))
.takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(), "PNG", new File("D:/selenium/" + System.currentTimeMillis() + ".png"));
}
在AppTest类中@Test使用后:
@AfterMethod
public void takeScreenShot() throws InterruptedException, IOException {
ScreenshotPage.ScreenshotPage1(driver);
}
我在下面发布了一些到目前为止已经完成的示例代码,我得到了一个异常。 基本类: 登录页面: LoginTest类: 在设置用户名和密码时,我得到了。你能帮帮我吗? 我是PageFactory的POM新手,所以我不知道如何解决这个问题,但如果有人能帮我解决这个问题,那将对我大有帮助。
要给工程获取支持,需要创建并交流一个能够证明这个组织整体的真正价值的愿景。试着让其他人分享他们对你的创造的愿景的观点。这给他们一个理由去支持你并给予你他们的智慧。独立地为你的工程补充关键的支持者。不论在什么可能的地方,展示,但不告诉。如果可能的话,构建一个原型或者一个模型来证明你的主意。一个原型总是有力的,但在软件中,它比任何书面的描述都要高级得多。
北京优锘科技有限公司 地址:北京市朝阳区酒仙桥路10号恒通国际商务园B23A 联系电话:400-666-9832 业务咨询:info@uino.com 售后服务:thingjs@uino.com
我还是想不通。几天前我的硒工作得很好;现在它给我带来了一些错误。我首先使用Nuget,然后我尝试手动安装它。 如何重现问题: 错误: OpenQA. Selenium.WebDriverExcture:抛出一个带有空响应的异常,向远程WebDriver服务器发送HTTP请求以获取URLhttp://localhost:60695/session.异常的状态为ReceiveFailure,消息为:基
问题内容: 我用来单击所需的网页,然后使用解析网页。 有人展示了如何在中获取元素的内部HTML。有没有办法获取整个页面的HTML?谢谢 中的示例代码 (基于上面的帖子,语言似乎没有太大关系): 问题答案: 要获取整个页面的HTML: 要获取外部HTML(包括标记): 要获取内部HTML(不包括标签):
2019-01-02 18:32:20,251错误O.A.J.T.JMeterThread:处理采样器时出错:'JP@GC-WebDriver采样器‘。IllegalArgumentException:浏览器尚未配置。请确保至少为ThreadGroup创建了1个WebDriverConfig。在com.googlecode.jmeter.plugins.webdriver.sampler.webd