2013-11-04 63 views
0

我是新來硒和已經寫代碼,我需要從中打開瀏覽器,但不能從Excel工作表中打開URL一個Excel工作表中讀取信息。我已經設置了正確的Excel路徑以及行號和列號。我正在使用testng並編寫測試用例。爲什麼在通過Excel工作表訪問數據時網頁未打開?

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"); 
     } 
    } 

我想知道我在哪裏出了問題。

+1

它會拋出任何錯誤嗎?你可以發佈日誌嗎? – Akbar

回答

0

我一直在想這個問題,並且在我看來是在使用.ods文件。如果我嘗試調用.xls文件的Excel getCellData方法,我可以找到鏈接。如果我嘗試使用包含相同材質的.ods文件,我找到一個空單元格。

我假設你正在使用POI,它是導致你的問題,而不是一個硒問題的POI的限制。

+0

Apache POI只適用於Microsoft Office文件格式,如果您的文件是'.ods',那麼POI不能也不會讀取它! – Gagravarr

相關問題