Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用testNG利用test as数组中的数据提供程序元素_Java_Arrays_Webdriver_Selenium Chromedriver_Testng - Fatal编程技术网

Java 使用testNG利用test as数组中的数据提供程序元素

Java 使用testNG利用test as数组中的数据提供程序元素,java,arrays,webdriver,selenium-chromedriver,testng,Java,Arrays,Webdriver,Selenium Chromedriver,Testng,我创建了一个selenium testNG webdriver自动化程序,它可以从excel表中获取数据,并使用这些数据填充一些字段和执行一些任务 因此,我编写了一个代码,在任务硬编码时可以成功执行,但我希望避免硬编码和@test中的大量重复,因此我插入了一个数组列表,希望在其中利用@DataProvider元素 这是密码 package com.mycompany.app; import java.util.ArrayList; import java.util.Iterator; impo

我创建了一个selenium testNG webdriver自动化程序,它可以从excel表中获取数据,并使用这些数据填充一些字段和执行一些任务

因此,我编写了一个代码,在任务硬编码时可以成功执行,但我希望避免硬编码和
@test
中的大量重复,因此我插入了一个数组列表,希望在其中利用@DataProvider元素

这是密码

package com.mycompany.app;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.excelreader.utility.getDataUtil;

public class testTab2 {
    static WebDriver driver;


    @BeforeTest
    public void beforeTest() 
    {
        System.setProperty("webdriver.chrome.driver", "C:\\Browsers drivers\\chromedriver.exe");
        driver = new ChromeDriver();

        driver.get("https://kdp.amazon.com/en_US/title-setup/paperback/new/details?ref_=kdp_BS_D_cr_ti");
        driver.findElement(By.xpath("//*[@id='ap_email']")).sendKeys("email");
        driver.findElement(By.xpath("//*[@id=\'ap_password\']")).sendKeys("password");
        driver.findElement(By.xpath("//*[@id=\"signInSubmit\"]")).click();
    }

    @BeforeMethod
    public void newTab() 
    {
        driver.switchTo().newWindow(WindowType.TAB);
        driver.get("https://kdp.amazon.com/en_US/title-setup/paperback/new/details?ref_=kdp_BS_D_cr_ti");

    }

    @Test(dataProvider = "getTestData")
    protected static void testAmazon1(String innerPath, String coverPath, String book_Title, String subtitle,
              String f_Name, String l_Name, String Description, String keyword1, String keyword2, 
              String keyword3, String keyword4, String keyword5, String keyword6, String keyword7, 
              String category_1, String category_2, String step_1, String step_2, String step_3, 
              String step_4, String step_5, String step_6, String step_7, String step_a, String step_b, 
              String step_c, String step_d, String step_e, String step_f, String step_g, String step_1final,String step_2final) throws Exception 
    {
        //driver = new ChromeDriver();
        Thread.sleep(4000);


        driver.findElement(By.xpath("//button[@id='data-print-book-categories-button-proto-announce']")).click();
        Thread.sleep(2000);



        String[] category_A = new String [7];

        category_A [0] = step_1;
        category_A [1] = step_2;
        category_A [2] = step_3;
        category_A [3] = step_4;
        category_A [4] = step_5;
        category_A [5] = step_6;
        category_A [6] = step_7;

        int i = 0;
        int index=0;
        while (i<=6) {
            if (category_A [i] != "kk") {
                i++;
            } else {
                index=i;
                System.out.println(index);
                break;
            }
        }

        for (int j=0; j<=index-2 ; j++) {

            String expand = driver.findElement(By.xpath("//div[@id='icon-" + category_A [j] + "']")).getAttribute("class");
            System.out.println("expand befor execution is: "+ expand);

            String icon_plus = "icon expand-icon";
            if(expand.equals(icon_plus)) {
                driver.findElement(By.xpath("//div[@id='icon-" + category_A [j] + "']")).click();}
                     else {
                        System.out.println(category_A [j] + " is expanded");
                    }
        }
        driver.findElement(By.xpath("//input[contains(@id,'"+ step_1final +"')]")).click();
        System.out.println("the following checkbox :" + step_1final + "is checked");

    }



      @DataProvider(parallel = false)
      public Iterator<Object[]> getTestData() {
          ArrayList<Object[]> testData = getDataUtil.getDataFromExcel();
          return testData.iterator();

      }

}
如果有人有解决办法,我需要解决这个问题。
非常感谢。

您没有提供所看到错误消息的详细信息,但查看您的代码,我猜您可能收到了如下所示的错误消息:

org.testng.internal.reflect.MethodMatcherException: 
[public void com.rationaleemotions.stackoverflow.qn59935793.DataProviderSample.testMethod(java.lang.String,java.lang.String)] has no parameters defined but was found to be using a data provider (either explicitly specified or inherited from class level annotation).
Data provider mismatch
Method: testMethod([Parameter{index=0, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=1, type=java.lang.String, declaredAnnotations=[]}])
Arguments: [(java.util.Collections$SingletonList) [a]]
您的测试代码有问题

您的测试方法具有以下特征:

@Test(dataProvider=“getTestData”)
受保护的静态void testAmazon1(字符串内部路径、字符串覆盖路径、字符串书籍标题、字符串副标题、,
字符串f_名称、字符串l_名称、字符串描述、字符串关键字1、字符串关键字2、,
字符串关键字3、字符串关键字4、字符串关键字5、字符串关键字6、字符串关键字7、,
字符串类别_1、字符串类别_2、字符串步骤_1、字符串步骤_2、字符串步骤_3、,
串级4、串级5、串级6、串级7、串级a、串级b、,
字符串步骤c、字符串步骤d、字符串步骤e、字符串步骤f、字符串步骤g、字符串步骤1最终、字符串步骤2最终)引发异常
{
但是您的数据提供程序正在返回一个
迭代器

基于数据提供程序中的以下代码行

ArrayList testData=getDataUtil.getDataFromExcel();

无法确定
getDataFromExcel()
方法的确切返回类型

但是按照您在测试方法中所解释的,我猜您的
getDataFromExcel()
可能正在返回一个列表,并且您正在尝试将其映射到测试方法中的各个元素

那不行

下面的示例演示了如何直接使用
List

导入java.util.array;
导入java.util.Collections;
导入java.util.Iterator;
导入java.util.List;
导入org.testng.annotations.DataProvider;
导入org.testng.annotations.Test;
公共类DataProviderSample{
@数据提供者(name=“dp”)
公共迭代器getData(){
返回数组.asList(
新对象[]{Collections.singletonList(“a”)},新对象[]{Arrays.asList(“x”,“y”)})
.iterator();
}
@测试(dataProvider=“dp”)
公共void测试方法(列表数据){
data.forEach(System.out::println);
}
}

您没有提供所看到错误消息的详细信息,但查看您的代码,我猜您可能收到了如下所示的错误消息:

org.testng.internal.reflect.MethodMatcherException: 
[public void com.rationaleemotions.stackoverflow.qn59935793.DataProviderSample.testMethod(java.lang.String,java.lang.String)] has no parameters defined but was found to be using a data provider (either explicitly specified or inherited from class level annotation).
Data provider mismatch
Method: testMethod([Parameter{index=0, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=1, type=java.lang.String, declaredAnnotations=[]}])
Arguments: [(java.util.Collections$SingletonList) [a]]
您的测试代码有问题

您的测试方法具有以下特征:

@Test(dataProvider=“getTestData”)
受保护的静态void testAmazon1(字符串内部路径、字符串覆盖路径、字符串书籍标题、字符串副标题、,
字符串f_名称、字符串l_名称、字符串描述、字符串关键字1、字符串关键字2、,
字符串关键字3、字符串关键字4、字符串关键字5、字符串关键字6、字符串关键字7、,
字符串类别_1、字符串类别_2、字符串步骤_1、字符串步骤_2、字符串步骤_3、,
串级4、串级5、串级6、串级7、串级a、串级b、,
字符串步骤c、字符串步骤d、字符串步骤e、字符串步骤f、字符串步骤g、字符串步骤1最终、字符串步骤2最终)引发异常
{
但是您的数据提供程序正在返回一个
迭代器

基于数据提供程序中的以下代码行

ArrayList testData=getDataUtil.getDataFromExcel();

无法确定
getDataFromExcel()
方法的确切返回类型

但是按照您在测试方法中所解释的,我猜您的
getDataFromExcel()
可能正在返回一个列表,并且您正在尝试将其映射到测试方法中的各个元素

那不行

下面的示例演示了如何直接使用
List

导入java.util.array;
导入java.util.Collections;
导入java.util.Iterator;
导入java.util.List;
导入org.testng.annotations.DataProvider;
导入org.testng.annotations.Test;
公共类DataProviderSample{
@数据提供者(name=“dp”)
公共迭代器getData(){
返回数组.asList(
新对象[]{Collections.singletonList(“a”)},新对象[]{Arrays.asList(“x”,“y”)})
.iterator();
}
@测试(dataProvider=“dp”)
公共void测试方法(列表数据){
data.forEach(System.out::println);
}
}

更新-输出

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[contains(@id,'self--help_self--management_general')]"}
  (Session info: chrome=79.0.3945.130)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '4.0.0-alpha-3', revision: '8c567de6dc'
System info: host: 'UNKNOUN-PC', ip: '192.168.1.102', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '13.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.130, chrome: {chromedriverVersion: 79.0.3945.36 (3582db32b3389..., userDataDir: C:\Users\moad\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:52780}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 22669af4ff7fb4956b5951fd1aa2f1c6
*** Element info: {Using=xpath, value=//input[contains(@id,'self--help_self--management_general')]}
 if (category_A [i] != "kk") {
我在if行之后添加了
System.out.println(category_a[I]+“index=“+I”);
,我发现数组实际上正在接收数据,但问题出在if-else条件中。我发现即使条件不满足,它也会继续执行它,直到最后一个元素

这是我得到的结果

nonfiction index = 0
self--help index = 1
self--help_self--management index = 2
kk index = 3
kk index = 4
kk index = 5
kk index = 6

我不知道这是什么原因?

更新-输出

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[contains(@id,'self--help_self--management_general')]"}
  (Session info: chrome=79.0.3945.130)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '4.0.0-alpha-3', revision: '8c567de6dc'
System info: host: 'UNKNOUN-PC', ip: '192.168.1.102', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '13.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.130, chrome: {chromedriverVersion: 79.0.3945.36 (3582db32b3389..., userDataDir: C:\Users\moad\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:52780}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 22669af4ff7fb4956b5951fd1aa2f1c6
*** Element info: {Using=xpath, value=//input[contains(@id,'self--help_self--management_general')]}
 if (category_A [i] != "kk") {
我在if行之后添加了
System.out.println(category_a[I]+“index=“+I”);
,我发现数组实际上正在接收数据,但问题出在if-else条件中。我发现即使条件不满足,它也会继续执行它,直到最后一个元素

这是我得到的结果

nonfiction index = 0
self--help index = 1
self--help_self--management index = 2
kk index = 3
kk index = 4
kk index = 5
kk index = 6

我不知道这是什么原因?

我只是用if语句替换它

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[contains(@id,'self--help_self--management_general')]"}
  (Session info: chrome=79.0.3945.130)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '4.0.0-alpha-3', revision: '8c567de6dc'
System info: host: 'UNKNOUN-PC', ip: '192.168.1.102', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '13.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.130, chrome: {chromedriverVersion: 79.0.3945.36 (3582db32b3389..., userDataDir: C:\Users\moad\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:52780}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 22669af4ff7fb4956b5951fd1aa2f1c6
*** Element info: {Using=xpath, value=//input[contains(@id,'self--help_self--management_general')]}
 if (category_A [i] != "kk") {
这是什么

 String breaker = "kk"
 if ( !category_A [i].contentEquals(breaker)) {
它是有效的,我仍然不知道为什么,但它是有效的,而且