简体   繁体   English

从数据库中获取值,并将该值用作Selenium Webdriver中的输入字段

[英]Fetching a value from the database and using that value as an input field in Selenium Webdriver

I am doing automation testing at my work and i'm stuck in a scenario. 我在工作中正在执行自动化测试,并且陷入了一个场景。

My first test consist of creating a new user account which creates a username and a password. 我的第一个测试包括创建一个新的用户帐户,该帐户创建一个用户名和一个密码。 PS My password is always the same PS我的密码总是一样的

My second test is basically login as a user with the username and password that I created in the first test. 我的第二个测试基本上是使用我在第一个测试中创建的用户名和密码作为用户登录。

Also, as the account is created, the database gets updated, and i'm able to connect to the database and execute the query (select user_name from accounts where password = 'pass123' order by created_dttm desc;) 另外,随着帐户的创建,数据库得到更新,并且我能够连接到数据库并执行查询(从创建密码为“ pass123”的帐户中选择“ user_name”,并按created_dttm desc顺序进行);

So, since i'm able to execute and get the value from the db, how can I fetch that value and insert it into my username field in order to login as a customer. 因此,由于我能够执行并从数据库获取值,因此我该如何获取该值并将其插入到我的用户名字段中,以便以客户身份登录。

Because i'm stuck in this situation, I always have to edit my 2nd test case with a different username that is created in my first tc. 因为我处于这种情况下,所以我总是必须用在我的第一个tc中创建的不同用户名来编辑第二个测试用例。 I would like my test script to take the username from the db and insert it into the username field automatically. 我希望我的测试脚本从数据库中获取用户名,并将其自动插入用户名字段。

I'm kinda new to Java and automation, therefore, any kind of help will be highly appreciated. 我对Java和自动化有点陌生,因此,将非常感谢您提供任何帮助。 Thanks a ton! 万分感谢!

You need to just pass the username value read from database to the sendKeys function of Webdriver. 您只需要将从数据库读取的用户名值传递给Webdriver的sendKeys函数。 For example, if you have stored the username value from database in String variable user and if you are using id property to recognize the username field, then you can enter that value in username field using following: 例如,如果您已将来自数据库的用户名值存储在字符串变量user中 ,并且您正在使用id属性识别用户名字段,则可以使用以下命令在用户名字段中输入该值:

driver.findElement(By.id("u")).sendKeys(user);

In the above code, u is the id of username field. 在上面的代码中, u是用户名字段的ID You can move your database access code to separate method as shown below: 您可以将数据库访问代码移动到单独的方法,如下所示:

public static String getUsernameFromDB() throws ClassNotFoundException, SQLException {
    //Accessing driver from the JAR file 
    Class.forName("oracle.jdbc.OracleDriver");
    System.out.println("Oracle JDBC driver loaded ok.");

    Connection con=DriverManager.getConnection("jdbc:oracle:thin:@151.120.92.150:1602:pppst1","ppp_app","ppp_app_pppst1");
    System.out.println("DB Connected Successfuly");

    Statement stmt = con.createStatement();

    ResultSet result = stmt.executeQuery("select user_name from accounts where password = 'pass123' order by created_dttm desc;");

    String account = null;
    while(result.next()){
        account = result.getString("USER_NAME");

        System.out.println("BAID: " + account);

        }
    con.close();
    return account;
}

Your test case code will become: 您的测试用例代码将变为:

String username = getUsernameFromDB();
driver.findElement(By.id("u")).sendKeys(username);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium WebDriver从查找字段中选择一个值 - How to select a value from a lookup field using selenium webdriver 如何使用 selenium webdriver 从输入类型下拉列表中选择一个值? - How to select a value from input type dropdown using selenium webdriver? 如何在Java中使用Selenium Webdriver获取更新的隐藏字段值 - how to get updated hidden field value using Selenium webdriver in java Selenium WebDriver:我想使用Java覆盖字段中的值 - Selenium WebDriver: I want to overwrite value in field using Java 使用 Selenium Webdriver 检索只读字段的值 - retrive value of read only field using Selenium Webdriver 从Selenium Webdriver WebElement字段中检索值并将其传递给Java变量 - Retrieving the value from a Selenium Webdriver WebElement field and passing it to a java variable 从数据库中获取值 - Fetching value from the database 使用 selenium webdriver 时如何从列中获取特定值并将其插入字段中 - How to get a specific value from column & insert it in field, when using selenium webdriver 无法使用Selenium WebDriver从应用程序第二个字段的自动填充值中选择值 - Unable to select value from the auto populated values for the second field in the application using selenium webdriver 使用 Selenium 和 Java 将值插入输入字段 - Insert value into input field using Selenium and Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM