0

我是 selenium 的新手,并且已经编写了代码,我需要从打开浏览器但不从 Excel 工作表打开 URL 的 Excel 工作表中获取信息。我已经设置了正确的 Excel 路径以及行号和列号。我正在使用 testng 并编写一个测试用例。

Excel程序是:

    public class Excel {
        public static int getRowCount(String xlPath,String shName){
            try{
                FileInputStream fis = new FileInputStream(xlPath);
                Workbook wb = WorkbookFactory.create(fis);
                Sheet sh = wb.getSheet(shName);
                return sh.getLastRowNum()+1;

            }catch(Exception e){
                Reporter.log(e.getMessage());
                return -1;
            }


        }

        public static String getCellData(String xlPath,String shName,int row,int cell)
        {
            try{
                FileInputStream fis = new FileInputStream(xlPath);
                Workbook wb = WorkbookFactory.create(fis);
                Sheet sh = wb.getSheet(shName);
                        return sh.getRow(row).getCell(cell).getStringCellValue();

                }catch (Exception e){
                Reporter.log(e.getMessage());
                return " ";
            }

        }

    }

获取单元格数据的程序是:

        public class Config {
            public WebDriver driver;
            public ProjectSpecific ps;
            @BeforeMethod
            public void preCondition() {
            String browser = Excel.getCellData("D:\\AutomationFW\\Actitime\\testdata\\config.ods", "Sheet1", 0, 0);
          if (browser.equals("ie"))
          {
                 System.getProperty("WebDriver.ie.Driver","D:\\AutomationFW\\Actitime\\drivers\\IE8-WindowsXP-x86-ENU.exe");
              driver = new InternetExplorerDriver();

          }else if (browser.equals("chrome"))
          {
              System.getProperty("WebDriver.chrome.Driver","D:\\AutomationFW\\Actitime\\drivers\\Firefox Setup Stub 24.0.exe");
              driver = new ChromeDriver();
          }else
          {
              driver = new FirefoxDriver();
          }


      String url=Excel.getCellData("D:\\AutomationFW\\Actitime\\testdata\\config.ods", "Sheet2", 0, 0);
      System.out.println("url is " +url);
      driver.get(url);
      Reporter.log("app is opening");
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      ps = new ProjectSpecific(driver);

    }

      @AfterMethod
      public void afterMethod() {
          driver.close();
          Reporter.log("app closed");

      }


    }

硒方法的程序是:

            public class ProjectSpecific {
        WebDriver driver;
        ProjectSpecific(WebDriver driver){
            this.driver = driver;
        }
        public void login(String un,String pwd)
        {
            driver.findElement(By.name("username")).sendKeys(un);
            driver.findElement(By.name("pwd")).sendKeys(pwd);
            driver.findElement(By.id("loginButton")).click();
            Reporter.log("logged in");
        }
        public void logout(){
            driver.findElement(By.className("logout")).click();
            Reporter.log("logged out");
        }

        public void verifyTitle(String expTitle)
        {
            String actTitle = driver.getTitle();
            Assert.assertEquals(actTitle, expTitle);
        }

        public void navigateToCustomers()
        {
            driver.findElement(By.className("label")).click();
            driver.findElement(By.linkText("Projects & Customers")).click();

            Reporter.log("navigated to Project&Customers");
        }

        public void ClickAddNewCustomer()
        {
             driver.findElement(By.xpath("//input [@value = 'Create New Customer']")).click();
        }

        public void addNewCustomer(String customerName)
        {
            driver.findElement(By.name("name")).sendKeys(customerName);
            driver.findElement(By.name("createCustomerSubmit")).click();
        }

        public void verifySuccessMessage(String expectedMsg)
        {
            String actualMsg =                     driver.findElement(By.className("successmsg")).getText();
            Assert.assertEquals(actualMsg, expectedMsg);
            Reporter.log("verify success Message");

        }

    }

第一个测试用例是:

    public class Testcase1 extends Config {
      @Test
      public void testCase1() {
         ps.login("admin", "uTASuga7");
         //Reporter.log("logged in");
         ps.verifyTitle("actiTIME - Login");
         //Reporter.log("verified Title");
         ps.logout();
         //Reporter.log("logged out");
      }
    }

我想知道我哪里出错了。

4

1 回答 1

0

我已经考虑过并进行了修改,在我看来,您正在使用 .ods 文件。如果我尝试getCellData调用 .xls 文件的 Excel 方法,我可以找到链接。如果我尝试使用包含相同材料的 .ods 文件,我会找到一个空单元格。

我假设您正在使用 POI,这是导致您的问题的 POI 的限制,而不是真正的硒问题。

于 2014-01-09T20:57:26.577 回答