当前位置: 首页 > 知识库问答 >
问题:

从Selenium中的dataprovider类传递值时出现“参数类型不匹配”错误

咸育
2023-03-14

当我试图使用@DataProvider类将excel表中的几个值传递给page objects类中的几个方法时,我遇到了“参数类型不匹配”错误。这些方法依次在@Test类中调用。你能在这个问题上帮助我吗。下文已提到该守则。

数据提供者

@DataProvider(name="ProductInfo")
  public static Object[][] productInfoDataprovider() throws Throwable {
    
	  File file = new File("C:/Users/chetan.k.thimmanna/Documents/Selenium/Resources/QAToolsECommerce/ECommerce_Data.xlsx");
	  FileInputStream fis = new FileInputStream(file);
	  XSSFWorkbook wb = new XSSFWorkbook(fis);
	  XSSFSheet sheet = wb.getSheet("Sheet1");
	  int lastRowNum = sheet.getLastRowNum();
	  Object[][] obj = new Object[lastRowNum][5];
	  for(int i=0; i<lastRowNum; i++){
		  
		  XSSFRow row = sheet.getRow(i+1);
		  obj[i][0]= row.getCell(0).getNumericCellValue();
		  obj[i][1]= row.getCell(1).getStringCellValue();
		  obj[i][2]= row.getCell(2).getNumericCellValue();
		  obj[i][3]= row.getCell(3).getStringCellValue();
		  obj[i][4]= row.getCell(4).getStringCellValue();
	  }
		fis.close();
	  return obj;
  }
public class QaToolsECommercePageObjects {
	
	WebDriver driver;
	
	/*Method to launch the browser and select the browser type */
	public void setBrowser(int browser){
		
		if(browser == '1'){
			driver = new FirefoxDriver();
		}else if(browser == '2'){
			System.setProperty("webdriver.chrome.driver", "C:/Users/chetan.k.thimmanna/Documents/Selenium/Resources/chromedriver.exe");
			driver = new ChromeDriver();
		}else if(browser == '3'){
			System.setProperty("webdriver.ie.driver", "C:/Users/chetan.k.thimmanna/Documents/Selenium/Resources/IEDriverServer.exe");
			driver = new InternetExplorerDriver();
		}
		//Maximize the window
		driver.manage().window().maximize();
		
		driver.get("http://store.demoqa.com/");
		//driver.get("http://toolsqa.com/");
		
		//browser load time
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}



	
	/* Searches the product required to purchase */
	public void searchProduct(String product){
		
		driver.findElement(By.name("s")).sendKeys(product);
		driver.findElement(By.name("s")).sendKeys(Keys.ENTER);
		//driver.findElement(By.linkText("Product Category")).click();	
	}
	

	
	/* Verifies the product name in the product search result and adds the product to the cart*/
	public void productVerify(String product){
		
		String productValue = driver.findElement(By.id("grid_view_products_page_container")).getText();
		boolean val = productValue.contains(product); //Value from excel sheet
		if(val == true){
			
			System.out.println("The product searched is found :" +productValue);
			//Click on add to cart  
			driver.findElement(By.name("Buy")).click();
			//Click on Go to check out
			driver.findElement(By.className("go_to_checkout")).click();	
		}else
		{
			System.out.println(" The product searched is not found :" +productValue);
		}
		
	}
	
	
	/* Verifies the product name, quantity, price and total price of the product */
	
	public void checkoutCartVerify(String product, int quantity, String prices, String totalPrices){
		
		WebElement cartTable = driver.findElement(By.className("checkout_cart"));
		List<WebElement> cartRows = cartTable.findElements(By.tagName("tr"));
		
		//Product name
		WebElement prodRow = cartRows.get(1);
		List<WebElement> prodCols = prodRow.findElements(By.tagName("td"));
		WebElement prodName = prodCols.get(1);
		String oriProdName = prodName.findElement(By.tagName("a")).getText();
		
		//Comparing product name 
		if(oriProdName.equals(product)){
			System.out.println("The Product searched and added to the cart is correct: "+oriProdName);
		}else
		{
			System.out.println("The product searched and added to the cart is incorrect: "+oriProdName);
		}
		
		
		//Quantity
		WebElement quantityCombo = prodCols.get(2).findElement(By.tagName("form"));
		List<WebElement> quantityVals = quantityCombo.findElements(By.tagName("input"));
		String prodQuantity = quantityVals.get(0).getAttribute("value");
		int pq = Integer.parseInt(prodQuantity);
		//Comparing product quantity 
		if(pq == quantity){
			System.out.println("The Product quantity added to the cart is correct: "+pq);
		}else
		{
			System.out.println("The product quantity added to the cart is incorrect: "+pq);
		}
		
		//Price
		String price = prodCols.get(3).getText();
		String[] priceSplit = price.split("\\.");
		String prodPrice = priceSplit[0];
		String priceFrac = priceSplit[1];
		System.out.println(price);
		
		
		//Comparing price of the quantity 
		if(priceFrac.equals("00")){
			if(prodPrice.equals(prices)){
				System.out.println("The Product price added to the cart is correct: "+prodPrice);
			}else{
				System.out.println("The product price added to the cart is incorrect: "+prodPrice);
			}
			
		}else
		{
			if(price.equals(prices)){
				System.out.println("The Product price added to the cart is correct: "+price);
			}else{
				System.out.println("The product price added to the cart is incorrect: "+price);
			}
			
		}
		
		//Total Price
		String totalPrice = prodCols.get(4).getText();
		String[] totalpriceSplit = totalPrice.split("\\.");
		String prodTotalprice = totalpriceSplit[0];
		String prodpriceFrac = totalpriceSplit[1];
		System.out.println(totalPrice);
		
		//Comparing Total Price of the quantity
		if(prodpriceFrac.equals("00")){
			if(prodTotalprice.equals(totalPrices)){
				System.out.println("The Product Total price added to the cart is correct: "+prodTotalprice);
			}else{
				System.out.println("The product Total price added to the cart is incorrect: "+prodTotalprice);
			}
		}else
		{
			if(totalPrice.equals(totalPrices)){
				System.out.println("The Product Total price added to the cart is correct: "+totalPrice);
			}else{
				System.out.println("The product Total price added to the cart is incorrect: "+totalPrice);
			}
		}
		
	}

测试类

public class QaToolsECommerceTest {
  @Test(dataProvider = "ProductInfo", dataProviderClass = QaToolsECommerceDataProvider.class)
  
  public void eCommerceProduct(int browser, String product, int quantity, String prices, String totalPrices) {
	  QaToolsECommercePageObjects qaEpo = new QaToolsECommercePageObjects();
	  
	  qaEpo.setBrowser(browser);
	  qaEpo.searchProduct(product);
	  qaEpo.productVerify(product);
	  qaEpo.checkoutCartVerify(product, quantity, prices, totalPrices);

  }

}

错误:

失败:eCommerceProduct(2.0,“Magic Mouse”,1.0,“$150”,“$150”)java.lang.IllegalArgumentException:在sun.reflect.nativeMethodAccessorImpl.Invoke0(本机方法)在sun.reflect.nativeMethodAccessorImpl.Invoke(未知源)在sun.reflect.delegatingMethodAccessorImpl.Invoke(未知源)在java.lang.reflect.Method.Invoke(未知源)在

共有1个答案

岳浩宕
2023-03-14

失败:eCommerceProduct(2.0,“Magic Mouse”,1.0,“$150”,“$150”)java.lang.IllegalArgumentException:参数类型不匹配

实际上,在方法eCommerceProduct()中,您期望参数为int字符串int字符串字符串,而实际参数为doubledouble字符串字符串

因此,您应该将方法ecommerceProduct()更改为:-

public void eCommerceProduct(double browser, String product, double quantity, String prices, String totalPrices) {
   -------
   -------
}

编辑:-

运行:C:\users\chetan.k.thimmanna\appdata\local\temp\testng-eclips e--1620381105\testng-customsuite.xml 1失败:eCommerceProduct(2,“Magic Mouse”,1,“$150”,“$150”)java.lang.nullpointerException at qatoolsecommerceExcel.qatoolsecommercePageObjects.java:42)

出现此错误是因为在ecommercodeProduct()方法内部调用qatoolsecommercepageObjects.setBrowser(浏览器);值,将browser值传递到intdouble,而在qatoolsecommercepageObjects.setBrowser(int浏览器)方法中,如果(浏览器=='1')表示字符串错误,则将其作为

您应该按以下方式修改qatoolsecommercePageObjects.setBrowser(int browser)方法:-

public class QaToolsECommercePageObjects {

  WebDriver driver;

  public void setBrowser(int browser){
        WebDriver driver
        if(browser == 1){
            driver = new FirefoxDriver();
        }else if(browser == 2){
            System.setProperty("webdriver.chrome.driver", "C:/Users/chetan.k.thimmanna/Documents/Selenium/Resources/chromedriver.exe");
            driver = new ChromeDriver();
        }else if(browser == 3){
            System.setProperty("webdriver.ie.driver", "C:/Users/chetan.k.thimmanna/Documents/Selenium/Resources/IEDriverServer.exe");
            driver = new InternetExplorerDriver();
        }
        //Maximize the window
        driver.manage().window().maximize();

        driver.get("http://store.demoqa.com/");
        //driver.get("http://toolsqa.com/");

        //browser load time
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
 -----
 -----
}
 类似资料:
  • 所以我从文件中读取信息,得到一个类型不匹配错误。我通过调试器运行了它,但我不确定是什么导致了它——因为它正在读取正确的数字(在本例中为2000),但它似乎不认为它是整数? 我的代码如下: 从这些信息中可以看出: 2014年雇员史密斯,约翰2000 2015推销员琼斯,比尔3000 100000 2014执行布什,乔治5000 55 2014年员工曼,莎拉4000 2015年销售员Marco,约旦5

  • 这是我运行程序时收到的错误: 注:[19533]是我使用的一个测试值。 这是在CustomerServiceBeanImpl.java中出现错误的方法: 在快速检查ERD时,“Customer”表中的“id”列的数据类型为bigint。然而,我不确定这是否重要。(顺便提一下PostgreSQL数据库。) 如何修复此错误?

  • 它打印出值的等效,这是因为这一行: 通过调用表示。 那么,如何使Hibernate相信是的实例? 我的枚举是由加载的。而由URLClassLoader加载,由另一个类加载器加载。

  • 我有一个带有8个JCombobox和文本字段的Jframe...当我按下提交按钮时得到了数据类型不匹配错误 如何解决这个错误?

  • 我做错了什么? 正在更新: 我发现了问题所在。问题与ActionRepository中找到的函数有关。函数的签名首先要求两个日期进行比较,然后id和我给出了相反的值。我很清楚,在我上了它之后,我会有一个问题的日期,所以答案确实帮助了我。谢谢大家!