我已经为同一文件编写了功能文件和单独的步骤定义文件。这里的主要工作是我要验证“注册”页面的元素。
但是当我执行功能文件时,“空指针异常”错误可以在此处看到。
我的功能文件是
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(即Web驱动程序wait)放到服务器上,但无法解决该错误。
我还发布了我的TestRunner类,以进一步说明。
请帮助我解决此错误。 了解NullPointerException错误是否存在需要帮助。
我应该增加webdriver的等待时间,以便它可以先跟踪元素然后执行所需的操作。
或者javaScriptExecutor中存在错误。