WebElement становится нулевым во время выполнения - PullRequest
0 голосов
/ 06 апреля 2020

Когда я запускаю этот код ниже, я получаю исключение нулевого указателя в нижней части, я запускаю это с помощью Cucumber и testng. почему WebElement у нас сбрасывается в ноль, когда я выполняю все эти шаги в одном go, хотя я хранился как 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 у нас становится нулевым во время выполнения? ниже приведен полный код

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"));


}

}

1 Ответ

0 голосов
/ 08 апреля 2020

Вы можете объявить нас как поле c.

private static WebElement we;

Но, на мой взгляд, создавать шаг огурца для предоставления элементов - плохая идея. Попробуйте прочитать о Page Object Model, которая делает ваш опыт автоматизации намного лучше!

...