获取硒支持页驱动程序DefaultElementLocator findElement nullpointerexception for driver

时间:2018-07-21 05:16:40

标签: java selenium-webdriver automation webdriver

我正在创建页面对象模型以及数据驱动程序框架。 我正在为登录编写Testcase,但是却得到pagefactory nullpointerexception。 1.如何初始化驱动程序以避免此错误? 2.我又如何在测试脚本中使用Sccreenshot页面类,下面给出了代码。

失败:日志(“ s@gmail.com”,“ sw45”) java.lang.NullPointerException     在org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)

TestBase类

公共类TestBase {     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 =新的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

公共类ScreenshotPage扩展了TestBase {     私有WebDriver驱动程序=新的C​​hromeDriver();

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

}

2 个答案:

答案 0 :(得分:0)

使此方法返回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);
    }

答案 1 :(得分:0)

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();
    
}
}