获得参数类型不匹配'从Selenium

时间:2016-09-06 07:10:18

标签: java selenium selenium-webdriver selenium-ide selenium-chromedriver

我面临的争论类型不匹配'尝试使用@dataprovider类将少量值从excel表传递到页面对象类中的少数方法时出错。反过来,这些方法在@test类中调用。你能帮我解决这个问题。代码已在下面提到。

的DataProvider



@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;
  }
&#13;
&#13;
&#13;

PageObjects

&#13;
&#13;
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);
			}
		}
		
	}
&#13;
&#13;
&#13;

测试类

&#13;
&#13;
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);

  }

}
&#13;
&#13;
&#13;

错误:

  

失败:eCommerceProduct(2.0,&#34; Magic Mouse&#34;,1.0,&#34; $ 150&#34;,&#34; $ 150&#34;)       java.lang.IllegalArgumentException:参数类型不匹配           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)           在java.lang.reflect.Method.invoke(未知来源)

3 个答案:

答案 0 :(得分:1)

  

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

实际上,在方法eCommerceProduct()中,您期望参数为intStringintStringString,而实际参数为传递为doubleStringdoubleStringString

所以你应该改变方法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 FAILED:eCommerceProduct(2,&#34; Magic Mouse& #34;,1,&#34; $ 150&#34;,&#34; $ 150&#34;)java.lang.NullPointerException:qaToolsECommerceExcel.QaToolsECommercePageObjects.setBrowser(QaToolsECommercePag eObjects.java:42)

发生此错误是因为您在QaToolsECommercePageObjects.setBrowser(browser);方法中调用了eCommerceProduct(),并在browser方法中将int值传递到doubleQaToolsECommercePageObjects.setBrowser(int browser)你将它比作if(browser == '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);
    }
 -----
 -----
}

答案 1 :(得分:1)

在dataprovider函数productInfoDataprovider()中,您有

  obj[i][0]= row.getCell(0).getNumericCellValue();
  obj[i][2]= row.getCell(2).getNumericCellValue();

getNumericCellValue()将返回参数设置为double,因为您将获得参数类型不匹配。

使用

键入将其强制转换为int
  obj[i][0]= (int)row.getCell(0).getNumericCellValue();
  obj[i][2]= (int)row.getCell(2).getNumericCellValue();

答案 2 :(得分:1)

当你启动浏览器时,你写了&#34;否则如果(浏览器==&#39; 2&#39;)&#34;。在这里你期望2作为字符,但你正在证明数据为int / Double。试试

else if(browser == 2 )

在此之前,将浏览器变量的double值转换为整数。