在Cucumber步骤定义中使用System.setProperty时出现多个标记错误

时间:2018-03-19 10:57:01

标签: java selenium selenium-webdriver webdriver cucumber

错误如下: -

Multiple markers at this line

- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error on token ".", @ expected after this token
- Syntax error, insert "Identifier (" to complete MethodHeaderName
- Syntax error on token ",", < expected
- Syntax error, insert "SimpleName" to complete QualifiedName

System.setProperty是jar文件的一部分或存在于何处?这样我就可以访问它并在我的程序中使用。

public class Loginstepdef {

        System.setProperty("webdriver.chrome.driver","E:\\Selenium\\chromedriver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();


        @Given("^I am on the login page of the application$")
        public void output()throws InterruptedException
        {
            driver.get("https://motzie-staging.mobile-recruit.com/login");
            //Navigation navigator=driver navigator();
            //navigator.to(http://10.10.5.56/login);

        }

        @When("^I login with username (.*) and password(.*)$")
        public void output2(String username, String password) throws InterruptedException 
        {
            //WebElement loginfield = driver.findElement(By.className("ng-scope"));
            WebElement loginfield = driver.findElement(By.id("username"));
            loginfield.sendKeys(username);
            loginfield.sendKeys(password);
            WebElement loginbutton = driver.findElement(By.className("ng-scope"));  
            loginbutton.click();
        }

        @Then("^Login successfully in that account$")
        public void output3() throws InterruptedException
        {
            System.out.print("login successfully");
        }

}              

4 个答案:

答案 0 :(得分:0)

\用于转义序列,因此它会给您一个错误。在路径

中使用/\\
System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver\\chromedriver.exe"); 

答案 1 :(得分:0)

System.setProperty 中的来自Java System Class Method,并且都接受字符串值。因此,按ctrl+space将无法获取最佳结果。

您看到的错误来自 Value 字段:

"E:\Selenium\chromedriver\chromedriver.exe"

您必须通过以下任一选项传递 WebDriver 变体的绝对路径:

  • 转义反斜杠(\\),例如"E:\\Selenium\\chromedriver\\chromedriver.exe"
  • 单个正斜杠(/),例如"E:/Selenium/chromedriver/chromedriver.exe"
  

注意:您可以在Exception in thread “main” java.lang.IllegalStateException:The path to the driver executable must be set by the : system property

中找到详细的讨论

更新

当您使用cucumber时,您必须将 WebDriver 的初始化部分放在方法范围内,如下所示:

WebDriver driver;

@Given("^Open Firefox and Start Application$")
public void Open_Firefox_and_Start_Application() throws Throwable 
{
    System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver\\chromedriver.exe");
    driver =  new ChromeDriver();
}

答案 2 :(得分:0)

因为写错了而引发了问题。使用“\\”“/”

(“webdriver.chrome.driver”,“E:\\ Selenium \\ chromedriver \\ chromedriver.exe”);

答案 3 :(得分:-1)

用主方法写:

public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver","C:\\Users\\admin\\Downloads\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();;
    String url ="https://www.gmail.com";
    driver.get(url);