문제

I am currently working on a testsuite that is maintained inside soapui. This testsuite also contains some web functionality tests that are implemented as HTTP.GET and HTTP.POST requests. I would like to port those tests to WebDriver. Inside SoapUI, there is a possibility to run a groovy script that basically gives you access to writing java code which I have been trying to do.

What I have at the moment is:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.By;


LoginPage loginPage = new LoginPage();
loginPage.login()

public class LoginPage {

    WebDriver driver = null;

    public WebDriver initDriver(){
        if (driver == null ){
        driver = new FirefoxDriver();
        driver.get("http://mysite")}
    }

//  @FindBy(id = "j_username") WebElement userName;
//  @FindBy(id = "j_password") WebElement password;
//  @FindBy(xpath = "//h2[contains(text(), \"kasutajanime\")]//..//..//input[@class=\"button\"]") WebElement btnSubmit
    public LoginPage(){

        driver = initDriver()
    //  PageFactory.initElements(driver, this);
    }


    public void login(){
        WebElement userName = driver.findElement(By.id("j_username"));
        WebElement password = driver.findElement(By.id("j_password"));
        WebElement btnSubmit = driver.findElement(By.xpath("//h2[contains(text(), \"kasutajanime\")]//..//..//input[@class=\"button\"]"));
        userName.sendKeys("SOAPUI")
        password.sendKeys("soapui")
        btnSubmit.click()       
}
}

When I run it, I get:

Fri Mar 07 13:09:44 EET 2014:ERROR:java.lang.NullPointerException: Cannot invoke method findElement() on null object
java.lang.NullPointerException: Cannot invoke method findElement() on null object at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:32) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) at LoginPage.login(Script52.groovy:34) at LoginPage$login.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112) at Script52.run(Script52.groovy:9) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:96) at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:149) at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:274) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

Anyway, even if I do:

public void login(){
        driver = initDriver()

Which shouldn't open new instance as I am checking if the driver exists or not, it still opens an new instance.

I have been trying to declare the driver instance as static, but with no luck.

If not writing a class but simply calling WebDriver routines, everything works. Has anybody ever tried something like that? Is it even possible or am I missing something obvious (soapUI does not offer too many debugging options)

도움이 되었습니까?

해결책

It seems that you shouldn't initialize your driver as WebDriver driver = null; It should be just WebDriver driver; In addition you write

public WebDriver initDriver(){
            if (driver == null ){
            driver = new FirefoxDriver();
            driver.get("http://mysite")}
        }

But if you want to have single browser you should have

public WebDriver initDriver(){
if (driver == null) {
   driver = new FirefoxDriver();
  }
  return driver;
}

다른 팁

In your constructor, you set driver to be:

    driver = initDriver()

Then in initDriver(), you set driver up if it's null

    if (driver == null ){
        driver = new FirefoxDriver()
        driver.get("http://mysite")
    }

But then don't return anything if it isn't null

This then sets driver to null in the constructor.

Try changing:

    driver = initDriver()

to

    initDriver()

in the constructor

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top