【问题标题】:How to open Different URLs(with different usernames and passwords) in Selenium如何在 Selenium 中打开不同的 URL(使用不同的用户名和密码)
【发布时间】:2021-08-17 22:34:39
【问题描述】:

我已成功从 Excel 文件中读取数据,该文件由三列(URL/FQDN、用户名、密码)组成。 此 Excel 表由我的各种服务器的 FQDN 组成。我有另一个功能需要在所有这些服务器上执行某些任务。 由于我对 Selenium/java 非常陌生,我将不胜感激有关如何在不同浏览器窗口上打开所有 url 并为每个窗口传递用户名和密码这一事实的任何帮助。稍后我可以尝试管理我需要对所有这些执行的任务/相同的操作 这是我到目前为止为阅读 Excel 表而编写的一段代码-:

公共类修改{

public static FileInputStream fis;
public static XSSFWorkbook wb;
public static XSSFSheet sheet;
public static String row;
public static XSSFCell cell;



public static void main(String[] args) {
    
    try {
        getCellData();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public static void getCellData() throws IOException
{
    fis=new FileInputStream("C:\\Users\\windows\\Desktop\\hostname.xlsx");
    wb=new XSSFWorkbook(fis);
    sheet=wb.getSheet("hostname");

    int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();

    //Create a loop over all the rows of excel file to read it

    for (int i = 0; i < rowCount+1; i++) {
        
        

        Row row = sheet.getRow(i);

        //Create a loop to print cell values in a row

        for (int j = 0; j < row.getLastCellNum(); j++) {

            //Print Excel data in console

            System.out.print(row.getCell(j).getStringCellValue() + " ");
            
            
            
        }

        System.out.println();
    }
}

}

【问题讨论】:

    标签: java excel selenium data-driven-tests


    【解决方案1】:

    首先查看函数的访问修饰符(总是非常重要的!)。仅在单个类中使用的函数应该始终是私有的。

    你想做的其实并不难实现。只需编写一个(私有)函数,并提供 3 个参数:url、用户名和密码。您将在 getCellData() 函数的 for 循环中调用此函数。关于您如何确切地启动一个新窗口(或我认为会更好的选项卡),应该非常简单。我将不得不谷歌!只需导入 Selenium WebDriver 库,然后读入它!有很多功能可以帮助您在这里实现目标。

    【讨论】:

      【解决方案2】:

      假设getCellData() 至少返回此实例的 URL 列表。

      一旦它被执行,你就会有这样的东西:

      代码:

      public String[] getCellData() {
              return new String[]{"https://www.google.co.in", "https://www.youtube.com", "https://stackoverflow.com"};
          }
      

      在新标签页中打开每个网址:

      @Test
      public void testSO() throws InterruptedException {
          try {
              String[] allUrls = getCellData();
              System.out.println(allUrls.length);
              int count = 1;
              for(String url : allUrls) {
                  driver.get(url);
                  String windowsBefore = driver.getWindowHandle();
                  JavascriptExecutor js = (JavascriptExecutor)driver;     
                  js.executeScript("window.open('');");
                  ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
                  driver.switchTo().window(tabs.get(count));
                  count = count + 1;
                  Thread.sleep(3);
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-05
        • 2013-07-24
        • 2013-01-26
        相关资源
        最近更新 更多