selenium - 执行功能文件时显示错误“空指针异常”
问题描述
我已经为同一个文件编写了一个功能文件和一个单独的步骤定义文件。这里的主要工作是我想验证“注册”页面的元素。
但是当我执行功能文件时,“空指针异常”错误可以在这里看到“。
我的功能文件是
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|
我的 Test Runner 课程是:
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.
实际结果 :-
NullPointerException error could be seen on a page.
请帮助我找到此错误的根本原因。找不到此错误的根本原因。我试过把 wait ie Web driver wait ,但无法解决错误。
为了进一步清晰,我还发布了我的 Test runner 课程。
请帮我解决这个错误。了解 NullPointerException 错误的存在需要帮助。
我是否应该增加 webdriver 的等待时间,以便它可以先跟踪元素然后执行所需的操作。
或者 javaScriptExecutor 中存在错误。
解决方案
推荐阅读
- autodesk-forge - 如何让 Autodesk Viewer LayerManager 正确地恢复状态
- python - python datetime打印与附加到列表显示不同的结果
- mysql - 通过最近添加的记录使用另一个表中的内连接进行更新
- dart - Angulardart降价指令不起作用
- php - PHP - mysqli 中的错误 1064 尝试进行选择查询
- html - 在 html 中覆盖正文样式
- angular - tinymce 角度模板替换变量 - 如何让变量重置并在每次负载时保持动态?
- php - 如何使可编辑输入框成为必填字段?
- java - 在理解递归顺序树遍历中缺少链接
- idris - 根据(进一步)证明证明类型相等