gpt4 book ai didi

java - WebElement 在运行时变为 null

转载 作者:行者123 更新时间:2023-12-01 17:34:33 24 4
gpt4 key购买 nike

当我运行下面的代码时,我在下面的部分收到空指针异常, 我在 Cucumber 和 testng 的帮助下运行它。为什么当我一次性运行所有这些步骤时,WebElement 会重置为 null,即使我存储为 we=dr.findElement(By.id("email"));

@When("^I get value from textbox using gettext$")
public void i_get_value_from_textbox_using_gettext() throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.out.println(we.toString());
System.out.println("Getting value from text "+we.getText());
//tbHelp.tbGetText(By.id("email"));
}

为什么WebElement在运行时会变成null?下面是完整的代码,

public class FbWebElementsStepDefn 
{

private WebDriver dr;
private BrowserHelper browserHelp;
private WebElement we;
private List<WebElement> totWes;
private TextBoxHelper tbHelp;

@Given("^Launching Chrome Browser and navigating to \"([^\"]*)\" to test$")
public void launching_Chrome_Browser_and_navigating_to_to_test(String url) throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.setProperty("webdriver.chrome.driver", "D:\\\\seleniumJar\\\\chromedriver.exe");
dr=new ChromeDriver();
dr.get(url);
browserHelp=BrowserHelper.getInstance(dr);
browserHelp.BrowserMaximize();
tbHelp=TextBoxHelper.getInstance(dr);

}

@Given("^I provide unique locator for webelement$")
public void i_provide_unique_locator_for_webelement() throws Throwable {
// Write code here that turns the phrase above into concrete actions
we=dr.findElement(By.id("email"));


}

@Then("^I should get desired webelement$")
public void i_should_get_desired_webelement() throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.out.println(we.toString());
}

@Then("^I close Browser$")
public void i_close_Browser() throws Throwable {
// Write code here that turns the phrase above into concrete actions
if(dr!=null)
{
dr.quit();
}
}
@When("^I give non unique locator$")
public void i_give_non_unique_locator() throws Throwable {
// Write code here that turns the phrase above into concrete actions
totWes= dr.findElements(By.tagName("input"));

}

@Then("^I should get list of elements$")
public void i_should_get_list_of_elements() throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.out.println("Total Elements "+totWes.size());
}
@When("^I enter value \"([^\"]*)\" in text box using sendkeys$")
public void i_enter_value_in_text_box_using_sendkeys(String value) throws Throwable {
// Write code here that turns the phrase above into concrete actions
//we=dr.findElement(By.id("email"));
//we.sendKeys(value);
tbHelp.tbEnterText(By.id("email"), value);
}

@When("^I get value from textbox using gettext$")
public void i_get_value_from_textbox_using_gettext() throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.out.println(we.toString());
System.out.println("Getting value from text "+we.getText());
//tbHelp.tbGetText(By.id("email"));
}

@Then("^I clear textbox$")
public void i_clear_textbox() throws Throwable {
// Write code here that turns the phrase above into concrete actions
//we.clear();
tbHelp.tbClearText(By.id("email"));


}

}

最佳答案

您可以将 we 声明为静态字段。

private static WebElement we;

但在我看来,创建一个 cucumber 步骤来提供元素是一个坏主意。尝试阅读有关页面对象模型的内容,这会让您的自动化体验变得更好!

关于java - WebElement 在运行时变为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61062891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com