WebElement在运行时变为null

huangapple 未分类评论44阅读模式
英文:

WebElement becomes null during runtime

问题

以下是您要翻译的内容:

当我运行下面的代码时,在下面的部分中出现空指针异常,我是通过Cucumber和TestNG来运行的。为什么当我一次性运行所有这些步骤时,WebElement we 在运行时会重置为 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 we 变为 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"));
    }
}
英文:

When I run this below code, i am getting null pointer exception on below part,
I am running this with help of Cucumber and testng. why WebElement we becomes reset to null when i run all these steps in one go, eventhough i stored as we=dr.findElement(By.id(&quot;email&quot;));

@When(&quot;^I get value from textbox using gettext$&quot;)
	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(&quot;Getting value from text &quot;+we.getText());
	  //tbHelp.tbGetText(By.id(&quot;email&quot;));
	}

Why WebElement we becomes null during runtime?
below is the complete code,

public class FbWebElementsStepDefn 
{

private WebDriver dr;
private BrowserHelper browserHelp;
private WebElement we;
private List&lt;WebElement&gt; totWes;
private TextBoxHelper tbHelp;

@Given(&quot;^Launching Chrome Browser and navigating to \&quot;([^\&quot;]*)\&quot; to test$&quot;)
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(&quot;webdriver.chrome.driver&quot;, &quot;D:\\\\seleniumJar\\\\chromedriver.exe&quot;);
	dr=new ChromeDriver();
	dr.get(url);
	browserHelp=BrowserHelper.getInstance(dr);
	browserHelp.BrowserMaximize();
	tbHelp=TextBoxHelper.getInstance(dr);
	
}

@Given(&quot;^I provide unique locator for webelement$&quot;)
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(&quot;email&quot;));
	
    
}

@Then(&quot;^I should get desired webelement$&quot;)
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(&quot;^I close Browser$&quot;)
public void i_close_Browser() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    if(dr!=null)
    {
    	dr.quit();
    }
}
@When(&quot;^I give non unique locator$&quot;)
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(&quot;input&quot;));
    
}

@Then(&quot;^I should get list of elements$&quot;)
public void i_should_get_list_of_elements() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    System.out.println(&quot;Total Elements &quot;+totWes.size());
}
@When(&quot;^I enter value \&quot;([^\&quot;]*)\&quot; in text box using sendkeys$&quot;)
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(&quot;email&quot;));
	//we.sendKeys(value);
	tbHelp.tbEnterText(By.id(&quot;email&quot;), value);
}

@When(&quot;^I get value from textbox using gettext$&quot;)
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(&quot;Getting value from text &quot;+we.getText());
  //tbHelp.tbGetText(By.id(&quot;email&quot;));
}

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

}

答案1

得分: 0

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

private static WebElement we;

但在我看来,为提供元素创建一个cucumber步骤是一个不好的主意。
尝试阅读关于页面对象模型的内容,它可以让你的自动化体验更加出色!

英文:

You can declare we as static field.

private static WebElement we;

But in my opinion it's a bad idea to create a cucumber step for providing elements.
Try to read about Page Object Model which makes your automation experience much better!

huangapple
  • 本文由 发表于 2020年4月6日 23:08:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/61062891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定