Java 获取错误:org.openqa.selenium.WebDriverException:未知错误:键应该是字符串

Java 获取错误:org.openqa.selenium.WebDriverException:未知错误:键应该是字符串,java,selenium,user-interface,testing,Java,Selenium,User Interface,Testing,从selenium UI中的属性文件访问数据时出错:如何解决此问题 org.openqa.selenium.WebDriverException: unknown error: keys should be a string. 我有以下框架。我是为了自己的安逸而做的,看起来让我自己变得复杂了。如果有什么好主意,请提出来。 感谢所有建议 框架包含以下内容: 1. 配置属性文件 2. 实用程序类 3. 页面元素定义类文件 4. 可重用函数类文件 5. 测试类 config.properties文件

从selenium UI中的属性文件访问数据时出错:如何解决此问题

org.openqa.selenium.WebDriverException: unknown error: keys should be a string.
我有以下框架。我是为了自己的安逸而做的,看起来让我自己变得复杂了。如果有什么好主意,请提出来。 感谢所有建议

框架包含以下内容:
1. 配置属性文件
2. 实用程序类
3. 页面元素定义类文件
4. 可重用函数类文件
5. 测试类

config.properties
文件包含以下内容:

url=http://some.com
Email=someuser
Password=somepassword
实用程序类(
浏览器调用
)以下代码:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class BrowserCalls {

    public WebDriver driver;
    public Properties configs = new Properties();
    public String pathToProperties = "path to config.properties file";

    public void invokeChromeBrowser() throws IOException {

        FileInputStream input = new FileInputStream(pathToProperties);
        pmsConfigs.load(input);
        System.setProperty("webdriver.chrome.driver", configs.getProperty("chromepath"));
        driver = new ChromeDriver();
        getAndMazimize();

    }

    private void getAndMazimize(){

        driver.get(configs.getProperty("url"));
        driver.manage().window().maximize();

    }


    public void closeChromeBrowser(){

        if(driver != null){

            driver.close();
        }

    }


}
import org.openqa.selenium.By;

public class LoginPageElements {


    //Login page elements
    public static By element1 = By.xpath("/html/head/link[1]");
    public static By username = By.xpath("//*[@id=\"login\"]/input/tr1/td1");
    public static By password = By.xpath("//*[@id=\"login\"]/input/tr2/td1");
    public static By submitButton = By.xpath("//*[@id=\"login\"]/input/tr3/td2/button");
    public static By title = By.xpath("/html/head/title");

}
页面元素定义类文件具有以下代码:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class BrowserCalls {

    public WebDriver driver;
    public Properties configs = new Properties();
    public String pathToProperties = "path to config.properties file";

    public void invokeChromeBrowser() throws IOException {

        FileInputStream input = new FileInputStream(pathToProperties);
        pmsConfigs.load(input);
        System.setProperty("webdriver.chrome.driver", configs.getProperty("chromepath"));
        driver = new ChromeDriver();
        getAndMazimize();

    }

    private void getAndMazimize(){

        driver.get(configs.getProperty("url"));
        driver.manage().window().maximize();

    }


    public void closeChromeBrowser(){

        if(driver != null){

            driver.close();
        }

    }


}
import org.openqa.selenium.By;

public class LoginPageElements {


    //Login page elements
    public static By element1 = By.xpath("/html/head/link[1]");
    public static By username = By.xpath("//*[@id=\"login\"]/input/tr1/td1");
    public static By password = By.xpath("//*[@id=\"login\"]/input/tr2/td1");
    public static By submitButton = By.xpath("//*[@id=\"login\"]/input/tr3/td2/button");
    public static By title = By.xpath("/html/head/title");

}
测试用例类要调用的功能定义类:

import com.automation.PageElements.LoginPageElements;
import com.automation.ReusableFunctions.BrowserCalls;

public class LoginFeature extends BrowserCalls {

    public void userLogin(){

        driver.findElement(LoginPageElements.element1);
        driver.findElement(LoginPageElements.username).sendKeys(configs.getProperty(Email));
        driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty(Password));
        driver.findElement(LoginPageElements.submitButton).click();

    }

}
测试用例类别如下:

import com.automation.ReusableFunctions.BrowserCalls;
import com.automation.Components.LoginFeature;
import org.testng.annotations.Test;

import java.io.IOException;

public class LoginTestCase1 extends BrowserCalls {

    @Test (description = "Verify application login")
    public void LoginTest() throws IOException {

        LoginFeature login = new LoginFeature();
        login.invokeChromeBrowser();
        login.userLogin();
        login.closeChromeBrowser();

    }

}

您可以检查这两条语句返回的内容,如下所示:

System.out.println(System.getProperty("Email"));
System.out.println(System.getProperty("Password"));
可能它们返回的是
null
,这就是为什么会出现错误

试试这个:

public void userLogin(){
        System.out.println(System.getProperty("Email")); // check statement return
        System.out.println(System.getProperty("Password")); // check statement return

        driver.findElement(LoginPageElements.element1);
        driver.findElement(LoginPageElements.username).sendKeys(configs.getProperty("Email"));
        driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty("Password"));
        driver.findElement(LoginPageElements.submitButton).click();

    }
PS


您可以检查这两条语句返回的内容,如下所示:

System.out.println(System.getProperty("Email"));
System.out.println(System.getProperty("Password"));
可能它们返回的是
null
,这就是为什么会出现错误

试试这个:

public void userLogin(){
        System.out.println(System.getProperty("Email")); // check statement return
        System.out.println(System.getProperty("Password")); // check statement return

        driver.findElement(LoginPageElements.element1);
        driver.findElement(LoginPageElements.username).sendKeys(configs.getProperty("Email"));
        driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty("Password"));
        driver.findElement(LoginPageElements.submitButton).click();

    }
PS


将这两行更改为:

driver.findElement(LoginPageElements.username).sendKeys(configs.getProperty(Email))
driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty(Password)); 
至:

driver.findElement(LoginPageElements.username).sendKeys(configs.getProperty("username"))  
driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty("password"));  
现在谈谈建议部分:

这个类中存在一些严重的问题:LoginPageElements,这是因为绝对xpath

例如:您正在使用以下xpath:
/*[@id=\“login\”]/input/tr3/td2/button
单击提交按钮

一个好的替代方法是建议您使用相对xpath,例如:

//button[text()='Submit']  // This may not work cause you have not shared the HTML for the submit button. Here I am just guessing.
不同的选择是:sendKeys(Keys.RETURN),当且仅当应用程序支持在提供
用户名和
密码后单击输入时

driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty("password"+Keys.RETURN));
尽管如@Andrei所建议,如果您严重依赖登录作为测试方法,那么您应该为提交按钮而不是
键编写一个相对xpath或任何其他定位器。返回

您的所有xpath都是绝对的,请尝试按以下顺序编写定位器:

  • id
  • 类名
  • 链接文本
  • partialLinkText
  • 标记名
  • css选择器
  • xpath(尽量做到相对而非绝对)

  • 将这两行更改为:

    driver.findElement(LoginPageElements.username).sendKeys(configs.getProperty(Email))
    driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty(Password)); 
    
    至:

    driver.findElement(LoginPageElements.username).sendKeys(configs.getProperty("username"))  
    driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty("password"));  
    
    现在谈谈建议部分:

    这个类中存在一些严重的问题:LoginPageElements,这是因为绝对xpath

    例如:您正在使用以下xpath:
    /*[@id=\“login\”]/input/tr3/td2/button
    单击提交按钮

    一个好的替代方法是建议您使用相对xpath,例如:

    //button[text()='Submit']  // This may not work cause you have not shared the HTML for the submit button. Here I am just guessing.
    
    不同的选择是:sendKeys(Keys.RETURN),当且仅当应用程序支持在提供
    用户名和
    密码后单击输入时

    driver.findElement(LoginPageElements.password).sendKeys(configs.getProperty("password"+Keys.RETURN));
    
    尽管如@Andrei所建议,如果您严重依赖登录作为测试方法,那么您应该为提交按钮而不是
    键编写一个相对xpath或任何其他定位器。返回

    您的所有xpath都是绝对的,请尝试按以下顺序编写定位器:

  • id
  • 类名
  • 链接文本
  • partialLinkText
  • 标记名
  • css选择器
  • xpath(尽量做到相对而非绝对)

  • 不久前,我在属性文件方面遇到了问题,问题是行分隔符,当我用Notepad++打开属性文件时,一切看起来都很好,但当用Notepad打开文件时,行分隔符丢失了。可能会有帮助

    不久前,我在属性文件上遇到问题,问题是行分隔符,当我用记事本+++打开属性文件时,一切看起来都很好,但当用记事本打开文件时,行分隔符丢失了。可能会有帮助

    不要在Excel工作表中使用Excel图标保存Excel表格。 使用Eclipse中的按钮保存Excel工作表。 请检查下面给出的图像链接:-

    不要将带有Excel图标的Excel表格保存在Excel工作表中。 使用Eclipse中的按钮保存Excel工作表。 请检查下面给出的图像链接:-

    我不会说,
    sendKeys(Keys.RETURN)
    更好,因为在这种情况下,提交按钮不会被测试,如果它是可点击的,并且按照预期工作。我认为大多数用户都会点击按钮,而不会使用Enter。@AndreiSuvorkov:这也是一个用例。如果我必须测试一个应用程序的不同模块,(通过不同的模块,我指的是内部部分),我就不会把登录看作是我的测试。登录测试是第一个测试,也是最重要的测试。若你们不能在应用程序中登录,你们就什么也做不了。自动化是关于模拟用户行为的。这就是为什么我更愿意找到按钮并点击它。@AndreiSuvorkov:是的,我同意,并用同样的方法更新了问题。现在,当我对一些模块进行回归时,我会选择登录作为测试方法吗?如果您在这种情况下,您已经以数据驱动的测试方法登录,您正在测试10-15个以上的用户名和密码,那么我将