簡體   English   中英

獲取硒支持 pagefactory DefaultElementLocator findElement nullpointerexception for driver

[英]Getting selenium support pagefactory DefaultElementLocator findElement nullpointerexception for driver

我正在創建頁面對象模型和數據驅動程序框架。 我正在為登錄編寫測試用例,但得到 pagefactory nullpointerexception。 1. 我如何初始化我的驅動程序以避免這個錯誤? 2. 再次我如何在我的測試腳本中截圖頁面類我在下面給出了代碼。

失敗:Log("s@gmail.com", "sw45") java.lang.NullPointerException at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)

測試庫類

公共類 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);
}

}

應用測試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 extends 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"));
}

}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM