WebElement在运行时变为空

问题描述 投票:0回答:1

[当我运行下面的代码时,我在下面的部分收到空指针异常,我正在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"));


}

}

java selenium selenium-webdriver cucumber-java
1个回答
0
投票

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

private static WebElement we;

但是我认为创建一个用于提供元素的黄瓜步骤是一个坏主意。尝试阅读有关页面对象模型的知识,它可以使您的自动化体验更好!

© www.soinside.com 2019 - 2024. All rights reserved.