【问题标题】:TestNG test not getting invokedTestNG 测试没有被调用
【发布时间】:2017-01-30 23:13:08
【问题描述】:

我正在尝试连接我的数据库,然后运行查询以通过 testNG 测试获得结果。

【问题讨论】:

  • TestNG 是否需要 @Test 注释? junit4 可以。
  • 为什么要 import org.junit.*; 进行 TestNG 测试?

标签: java eclipse selenium selenium-webdriver testng-eclipse


【解决方案1】:

如果您打算使用 TestNG 来执行,请从类中删除所有 junit 导入。我建议您使用以下方法:

package regressionTesting;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;

import javax.naming.spi.DirStateFactory.Result;

import org.openqa.selenium.*;
import org.openqa.selenium.By.ByXPath;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestDBConnect {

public static String username = null;

    @BeforeClass
    public void setUp() {
        username = getUsernameFromDB();
    }

    @Test
    public void testUI() {

        //Some code for navigating to the required page
        driver.findElement(By.id("u")).sendKeys(username);
        //Code for executing remaining steps 

    }

    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.117.87.205:1602:epwfst1","epwf_app","epwf_app_epwfst1");
            System.out.println("DB Connected Successfuly");

            Statement stmt = con.createStatement();

            ResultSet result = stmt.executeQuery("select billing_application_accnt_id from epwf.payment where created_user_nm = '1600STOUTST10001'");

            String account = null;       
            while(result.next()){
                account = result.getString("BILLING_APPLICATION_ACCNT_ID");
                System.out.println("BAID: " + account);
            }
            con.close();
            return account;
         }
   @AfterClass
   public void cleanUp() {
          //Code for clean-up activities like closing browser and releasing resources.
   }
}

在上面的代码中,我假设 u 是您要在其中输入从数据库读取的值的文本字段的 id。您可以将其替换为实际的 id 属性值,或使用其他属性来识别用户名文本框。

【讨论】:

  • 使用上面的代码并删除额外的导入实际上确实解决了问题......但是当我调用该方法来获取用户名并插入到 sendkeys 时,它不起作用。下面是代码
  • String username = getUsernameFromDB(); driver.findElement(By.id("baid")).sendKeys(username); 我可以看到正在调用 db connect 方法并且正在执行查询,但是输入字段没有填满结果。 ??!?
  • 没有例外......输入字段只是空的,我的测试只是超时......Oracle JDBC driver loaded ok. DB Connected Successfuly BAID: PPB11007406 [Utils] Attempting to create C:\Users\Umair\Default test.xml [Utils] Directory C:\Users\Umair\Default suite exists: true PASSED: testUI FAILED: testChangeOrder它失败了,因为它超时了。
  • 实际上它现在正在工作....问题是我没有正确获得该输入字段的 id.. 一旦我得到它。它工作!非常感谢@Mahipal 的帮助!!
猜你喜欢
  • 1970-01-01
  • 2020-01-21
  • 2014-02-04
  • 2021-09-15
  • 2019-10-08
  • 1970-01-01
  • 2020-01-01
  • 2021-04-18
  • 2018-03-27
相关资源
最近更新 更多