为什么我无法在Selenium方法中返回值?

时间:2019-02-14 02:43:03

标签: java selenium selenium-webdriver

我已经创建了一个可以在excel中返回值的方法,但是当我运行调用该方法的主脚本时,该值无法返回。也许我的代码有问题,但是我不知道我需要修复哪一部分。

这是调用方法的脚本。

  GetExcel res = new GetExcel();
	String maker = res.getExcel("IS Maker Username");
	GetExcel res1 = new GetExcel();
	String passMaker = res1.getExcel("IS Maker Password");
	WebDriverWait wait = new WebDriverWait(driver, 20);
	wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='userName']")));
	driver.findElement(By.xpath("//input[@name='userName']")).click();
	driver.findElement(By.xpath("//input[@name='userName']")).sendKeys(maker);
	driver.findElement(By.xpath("//input[@name='password']")).click();
	driver.findElement(By.xpath("//input[@name='password']")).sendKeys(passMaker);
	driver.findElement(By.name("submitLogin")).click();		

这是方法功能

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.testng.annotations.Test;
@Test
public class GetExcel {
	
	private XSSFWorkbook wb;
	
	public String getExcel(String getName) throws IOException {
		
		File src = new File("C:\\selenium\\ExcelData\\TestData.xlsx");
		FileInputStream fis = new FileInputStream(src);
		wb = new XSSFWorkbook(fis);
		XSSFSheet sh1 = wb.getSheetAt(0);
	
		String getValue = null;
		String value = null;
		for (int i = 1; i<=1000; i++) {
			if(sh1.getRow(i).getCell(0) != null)
			{
				 getValue = sh1.getRow(i).getCell(0).getStringCellValue();
			}
	    	
	    	if(getValue != null) {
	    		if(getValue.contains(getName)) {
	    			if(sh1.getRow(i).getCell(1) != null)
	    			{
	    				 value = sh1.getRow(i).getCell(1).getStringCellValue();
	    			}
	        		System.out.println(value);
	        		fis.close();
	        		break;
	        	} 	
	    	}	
	    	
	    }
		return value;
		
		
		
	}

}  

方法功能是从不同的包中导入的

1 个答案:

答案 0 :(得分:1)

1。您的代码将始终返回空值,因为您正在将单元格值与excel sheet_Name进行比较。

2。当您在第二列中返回值时,它不应为空。

3。如果您尝试获取任何数字值,则应在像'12345这样的单元格中的数字前加上'

  1. 修改了代码,现在尝试。

    public String getExcel(String getName) throws IOException {

        File src = new File("C:\\selenium\\ExcelData\\TestData.xlsx");
        FileInputStream fis = new FileInputStream(src);
        wb = new XSSFWorkbook(fis);
        XSSFSheet sh1 = wb.getSheet(getName);
    
        String getValue = null;
        String value = null;
        int number=sh1.getLastRowNum()+1;
        System.out.println("Total used rows :" + number);
        for (int i = 1; i<number; i++) {
            if(sh1.getRow(i).getCell(0) != null)
            {
                 getValue = sh1.getRow(i).getCell(0).getStringCellValue();
            }
    
            if(getValue != null) {
                if(!getValue.contains(getName)) {
                    if(sh1.getRow(i).getCell(1) != null)
                    {
                         value = sh1.getRow(i).getCell(1).getStringCellValue();
                    }
                    System.out.println(value);
                    fis.close();
                    break;
                }   
            }   
    
        }
        return value;
    
    
    
    }
    

这是主要功能调用。

public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        GetExcel res = new GetExcel();
        String maker = res.getExcel("IS Maker Username");
        System.out.println("User :" + maker);
        GetExcel res1 = new GetExcel();
        String passMaker = res1.getExcel("IS Maker Password");
        System.out.println("Password :" + passMaker);
    }

请让我知道它是否对您有用。