如何解决:cucumber.runtime.CucumberException:无法实例化类AppiumdemoDj.SignUpDj

时间:2017-12-07 13:24:31

标签: java android automation cucumber appium

我已经检查了堆栈溢出中其他类似问题的答案,但没有一个帮助。你能帮我解决一下这个问题吗?我正在尝试自动化移动应用程序。学习android app自动化的东西。我的代码如下:

The project structure

功能文件:

Feature: Register for Yammer

Scenario: Sign Up for Yammer App

Given The App is launched
When I click on SignUp button
Then I should see FirstName Lastname EmailId Password field
When I click on SignUp button
Then I should see error

StringsDj.java类

package AppiumdemoDj;


public class StringsDj {
    public static String signUpButton = "com.yammer.v1:id/signup_button";
    public static String firstName = "com.yammer.v1:id/first_name_edittext";
    public static String lastName = "com.yammer.v1:id/last_name_edittext";
    public static String emailId = "com.yammer.v1:id/work_email_edittext";
    public static String password = "com.yammer.v1:id/password_edittext";

}

SignUpDj.java类

package AppiumdemoDj;

import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import cucumber.api.java.en.Given;      
import cucumber.api.java.en.Then;       
import cucumber.api.java.en.When;
import io.appium.java_client.AppiumDriver;


public class SignUpDj {

    private WebDriver driver;
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //Taking screenshot       

     @Given("^The App is launched$")                
        public void the_App_is_launched() throws Throwable                          
        {  
            DesiredCapabilities capabilities = new DesiredCapabilities();
            //capabilities.setCapability("webdriver.gecko.driver", "/Applications/Automation/dependencies/geckodriver");
            capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
            capabilities.setCapability("deviceName", "Pixel");
            capabilities.setCapability("platformVersion", "8.0.0");
            capabilities.setCapability("platformName", "Android");
            capabilities.setCapability("appPackage", "com.yammer.v1");
            capabilities.setCapability("appActivity", "com.yammer.droid.ui.LauncherActivity");

            //AppiumDriver wd = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
            //wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

            driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
            driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
        }

     @When("^I click on SignUp button$")                
        public void i_click_on_SignUp_Button() throws Throwable
        {

            driver.findElement(By.id(StringsDj.signUpButton)).click();
            FileUtils.copyFile(scrFile, new File("./ScreenShots/"));

         }

     @Then("^I should see FirstName Lastname EmailId Password field$")              
        public void i_should_see_FirstName_Lastname_EmailId_Password_field() throws Throwable
        {

            driver.findElement(By.id(StringsDj.firstName)).sendKeys("Test");
            FileUtils.copyFile(scrFile, new File("./ScreenShots/"));

            driver.findElement(By.id(StringsDj.lastName)).sendKeys("Dj");
            FileUtils.copyFile(scrFile, new File("./ScreenShots/"));

            driver.findElement(By.id(StringsDj.emailId)).sendKeys("reachdipanjan@gmail.com");
            FileUtils.copyFile(scrFile, new File("./ScreenShots/"));

            driver.findElement(By.id(StringsDj.password)).sendKeys("TestDj");
            FileUtils.copyFile(scrFile, new File("./ScreenShots/"));

         }

     @Then("^I should see error$")              
        public void i_should_see_error() throws Throwable
        {

         System.out.println("error displayed");

        }
}

收到的错误:

cucumber.runtime.CucumberException: Failed to instantiate class AppiumdemoDj.SignUpDj
    at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:47)
    at cucumber.runtime.java.DefaultJavaObjectFactory.getInstance(DefaultJavaObjectFactory.java:33)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
    at cucumber.runtime.Runtime.runStep(Runtime.java:300)
    at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
    at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
    at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:102)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:95)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:38)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.api.junit.Cucumber.run(Cucumber.java:100)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:41)
    ... 32 more
Caused by: java.lang.NullPointerException
    at AppiumdemoDj.SignUpDj.<init>(SignUpDj.java:25)
    ... 37 more

1 个答案:

答案 0 :(得分:0)

在SignUpDj类的第25行,驱动程序对象为null,因为它尚未初始化:

var testemails=["name.@gmail.com,santosh@gmail.com"];
var mailOptions = {     
    from:config.email_from_addr,        
    //bcc :receiver_email,
    bcc:testemails,
    subject :subject,
    html:html
};      
var transporter = nodemailer.createTransport(smtpTransport({
    host:config.email_host_name,
    port: 25
}));
transporter.sendMail(mailOptions, function(error, info){    
    console.log("error: "+error);
    console.log("info: "+info);
    if(error){
        //console.log("Rejected: "+info.rejected);
        console.log("error",'Failed to send: '+subject+' ; Error: '+error);         
        return callback(null);
    }else{
        console.log("info","Promotion_EmailFurnished:  "+subject+" : "+receiver_email); 
        return callback(null);
    }
});

您需要在需要截屏时调用该代码,而不是在类初始化时调用。我不确定您使用以下代码尝试实现的目标:

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //Taking screenshot       

但这不会截图。您需要结束调用以在方法中截取屏幕截图并调用该方法。