Selenium “一个错误”;空指针异常";在执行要素文件时显示
我已经编写了一个功能文件&一个单独的步骤定义文件。这里的主要工作是我想验证一个“注册”页面的元素 但当我执行该功能文件时,可以在这里看到“空指针异常”错误 我的功能文件是Selenium “一个错误”;空指针异常";在执行要素文件时显示,selenium,cucumber,bdd,Selenium,Cucumber,Bdd,我已经编写了一个功能文件&一个单独的步骤定义文件。这里的主要工作是我想验证一个“注册”页面的元素 但当我执行该功能文件时,可以在这里看到“空指针异常”错误 我的功能文件是 Feature: Verify the Register page @TC2 Scenario: Verify the Register link provided on Home Page Given user is on HomePage
Feature: Verify the Register page
@TC2
Scenario: Verify the Register link provided on Home Page
Given user is on HomePage
When user clicks on register link
Then user should be able to view <fields> such as
|fields|
|First Name|
|Last Name|
|Address|
|City|
|State|
|Zip code|
|Phone|
|SSN|
|Username|
|Password|
|Confirm|
我的步骤定义文件(代码)如下所示:
实际结果:-
在页面上可以看到NullPointerException错误。
请帮助我查找此错误的根本原因。无法找到此错误的根本原因。
我已尝试放置等待,即Web驱动程序等待,但无法解决错误。为了更清楚,我还发布了我的测试跑步者课程 请帮我解决这个错误。 了解NullPointerException错误的存在需要帮助 我是否应该增加webdriver的等待时间,以便它可以首先跟踪元素,然后执行所需的操作
或者javaScriptExecutor中存在错误。您的代码有很多问题。不要将步骤和页面对象混合在一起。我没有看到初始化用
@FindBy
注释的Web元素的PageFactory
的初始化。我没有看到ParaBank\u TC\u 02\u步骤中的WebDriver
的初始化ode>驱动程序尚未初始化,因为在其他步骤定义文件中,我已使用相同的功能关键字再次初始化驱动程序,即@Given(“^user is on HomePage$”)public void user_HomePage(){configFileReader=new configFileReader();System.setProperty(“webdriver.chrome.Driver”,“D:\\chromedriver.exe”);driver=new ChromeDriver();driver.get(“);driver.manage().window().maximize();}我列出的其他问题呢?您在哪里初始化了PageFactory
PageFactory.initElements(驱动程序,页面对象)
?public void Para_bank_TC_02_步骤(WebDriver驱动程序){PageFactory.initElements(驱动程序,此);}如果将页面对象从步骤类中分离出来,它将起作用。您的代码有很多问题。不要将步骤和页面对象混合在一起。我没有看到初始化用@FindBy
注释的WebElements的PageFactory
的初始化。我没有看到ParaBank_TC_02_步骤中的WebDriver
的初始化,因为在其他步骤定义文件中,我已经初始化了驱动程序,再次使用相同的功能关键字,即@Given(“^user is on HomePage$”)public void user_HomePage(){configFileReader=new configFileReader();System.setProperty(“webdriver.chrome.driver”,“D:\\chromedriver.exe”);driver=new chromedriver();driver.get(“);driver.manage().window().maximize();}我列出的其他问题呢?您在哪里初始化了PageFactory
?PageFactory.initElements(驱动程序,页面对象)
?public void Para_bank_TC_02_Step(WebDriver driver){PageFactory.initElements(driver,this);}如果将页面对象从Step类中分离出来,它将工作
package runner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
features="D:\\Para_Bank_Cucumber\\Cucumber_Framework\\src\\main\\java\\ParaBank\\Cucumber_Framework\\Para_bank_TC_01.feature"
, glue= {"StepFiles"}
, dryRun= false
,monochrome=true
, strict=true
,tags= {"@TC2"}
)
public class Test_runner {
}
package StepFiles;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import DataProvider.ConfigFileReader;
import cucumber.api.DataTable;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class ParaBank_TC_02_Step {
public WebDriver driver;
ConfigFileReader configFileReader;
@FindBy(xpath="//a[contains(text(),'Register')]")
private WebElement register_link;
@When ("^user clicks on register link$")
public void click_register() throws InterruptedException
{
WebDriverWait wait=new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOf(register_link));
//wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[contains(text(),'Register')]")));
register_link.click();
}
@FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.firstName']")
private WebElement first_name;
@FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.lastName']")
private WebElement last_name;
@FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.address.street']")
private WebElement Address;
@FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.address.city']")
private WebElement city;
@FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.address.state']")
private WebElement state;
@FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.address.zipCode']")
private WebElement zip;
@FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.phoneNumber']")
private WebElement phone;
@FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.ssn']")
private WebElement SSN;
@FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.username']")
private WebElement Username;
@FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.password']")
private WebElement password;
@FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='repeatedPassword']")
private WebElement repeat_pass;
@Then ("^user should be able to view <fields> such as$")
public void verify_fields(DataTable testData)
{
List<String> all_fields=testData.asList(String.class);
for(String fields_links:all_fields)
{
WebDriverWait wait =new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOf(first_name));
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", first_name);
wait.until(ExpectedConditions.visibilityOf(last_name));
js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", last_name);
wait.until(ExpectedConditions.visibilityOf(Address));
js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", Address);
wait.until(ExpectedConditions.visibilityOf(city));
js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", city);
wait.until(ExpectedConditions.visibilityOf(state));
js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", state);
wait.until(ExpectedConditions.visibilityOf(zip));
js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", zip);
wait.until(ExpectedConditions.visibilityOf(phone));
js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", phone);
wait.until(ExpectedConditions.visibilityOf(SSN));
js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", SSN);
wait.until(ExpectedConditions.visibilityOf(phone));
js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", phone);
wait.until(ExpectedConditions.visibilityOf(Username));
js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", Username);
wait.until(ExpectedConditions.visibilityOf(password));
js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", password);
wait.until(ExpectedConditions.visibilityOf(repeat_pass));
js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", repeat_pass);
}
}
}
1. it should click on Register link.
2. It should highlight the elements described in javascript executor.