从txt文件中读取一个字符串,并使用webdriver / java将其与下拉列表中的项目进行比较

时间:2015-12-22 10:58:56

标签: java selenium selenium-webdriver

以下是该方案:

1)创建一个输入字符串= 2015年9月的文件 2)将下拉列表收集到一个数组中 3)如果数组等于字符串出来循环,则下载新月报告并用新月份名覆盖txt文件。

我试过下面的代码,但我无法实现txt比较部分和txt覆盖部分,请帮助。

driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
          //maximizing the window
          driver.manage().window().maximize();

          List<WebElement> options;
          int i = 0;
          do
          {
              options = driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue")).findElements(By.tagName("option"));
              if(options.get(i).getText().equals("Sep 2015 (Unconventional wells)"))
              {

                  System.out.println("old month");
                  break;
              }
              else
              { if (options.get(i).getText().equalsIgnoreCase("All" )){

                  System.out.println("Download new month");

                  WebElement identifier = driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl03_ddValue']"));
                Select select1 = new Select(identifier);

                  //select1.selectByVisibleText("Oct");

                 select1.selectByVisibleText("Oct 2015 (Unconventional wells)");


                  Wait(20000);
                  driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl00']")).click();
                  Wait(70000);
                  //Click on File save button
                  driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Button']")).click();
                  //wait time to load the options
                  Wait(20000);
                  driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Menu']/div[2]/a")).click();
                  //fprofile.setPreference( "browser.download.manager.showWhenStarting", false );
                  //fprofile.setPreference( "pdfjs.disabled", true );
                  Wait(10000);
                  String str=options.get(2).getText();
                  System.out.println("str: " + str);

                 // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                  System.out.println("Oct month data downloaded in csv format");
                  //System.out.println("New month");
              }
              } } while (i++ < options.size());
    }

1 个答案:

答案 0 :(得分:1)

尝试这样:

//全局变量:

private WebDriver driver;
private String fileName = "/home/saritha/Desktop/MySeleniumFile.txt";
private File file;

在Tesng方法中:

@Test
public void oilGasTestng() throws InterruptedException {
    driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
    WebElement mSelectElement = driver
            .findElement(By
                    .xpath("//select[@id='ReportViewerControl_ctl04_ctl03_ddValue']"));
    List<WebElement> optionsList = mSelectElement.findElements(By
            .tagName("option"));

    for (int i = 2; i < optionsList.size(); i++) {
        WebElement element = optionsList.get(i);
        String newMonth = element.getText();
        /*
         * First we have read the data from file, if the file is empty then
         * download the file and save the downloaded month(which is old
         * month when v done with the downloading).
         */
        String oldMonth = "";
        if (i > 2) {
            oldMonth = getTheOldMonthFromFile();
        }
        System.out.println("Old Month= " + oldMonth + " NewMonth= "
                + newMonth);
        if (newMonth.equals(oldMonth)) {
            // IF the string are same, nthng we need to do
        } else if (!newMonth.equals(oldMonth)) {
            /*
             * If the string are not same,then i.e., considered as new
             * Month, download the new month details
             */
            element.click();
            driver.findElement(
                    By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl00']"))
                    .click();
            System.out.println(newMonth
                    + " month data downloaded in csv format");
            saveIntoAFile(newMonth);
            /*
             * You can which is oldMonth which is new month, by unCommenting
             * below condition
             */
            // if (i == 4)
            break;
        }
    }
}


//Save data into a file
 private void saveIntoAFile(String oldMonth) {
    BufferedWriter bw = null;
    if (oldMonth != null) {
        file = new File(fileName);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            Writer writer = new FileWriter(file);
            bw = new BufferedWriter(writer);
            bw.write(oldMonth);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

//Get the oldMonth string from the file
private String getTheOldMonthFromFile() {
    if (file == null && !file.exists()) {
        return null;
    }
    String oldMonth = "";
    StringBuffer strBuffer = new StringBuffer();
    BufferedReader br = null;
    java.io.FileReader reader = null;
    try {
        reader = new java.io.FileReader(file);
        br = new BufferedReader(reader);
        while ((oldMonth = br.readLine()) != null) {
            strBuffer.append(oldMonth);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null) {
                br.close();
            }
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return strBuffer.toString();
}