java / webdriver中的属性文件

时间:2016-01-23 10:36:30

标签: java selenium-webdriver

如何为以下代码创建属性文件:

刮刮:

    import java.util.List;

    import org.openqa.selenium.By;
    import org.openqa.selenium.StaleElementReferenceException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxProfile;

    public class stack10 {

     /*
     * Main method starting our execution
     * @param args is String type if we will pass any value it accept as array
     */

public static void main(String[] args)  {


    stack10 pa=new stack10();

        try {

            // starting process here , which downloads the latest month's data
            pa.oilGasReport();

             } catch (Exception e) {

                // TODO: handle exception
              e.printStackTrace();
            }

            }

    /*
     * oilGasReport method going to read Pennsylvania Department of Environmental Protection latest month data
     * and downloads the data to csv file
    */
public void oilGasReport() throws InterruptedException {

          // Changing default file downloading location  path using the FirefoxProfile.setpreference method.
          FirefoxProfile fprofile = new FirefoxProfile();   
          fprofile.setPreference("browser.download.folderList",2);
          fprofile.setPreference("browser.download.manager.showWhenStarting",false);
          //file download path
          fprofile.setPreference("browser.download.dir", "E:\\");
          //CSV format to download the data
          fprofile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");
          //Automatically downloads the file without manual selection
          fprofile.setPreference("browser.helperApps.alwaysAsk.force",false);
          fprofile.setPreference("browser.download.manager.showAlertonComplete",false);
          fprofile.setPreference("browser.download.manager.closeWhenDone",false);
          //Assigning the created profile to Firefox driver
          WebDriver driver=new FirefoxDriver(fprofile); 
          //opening the Pennsylvania Department of Environmental Protection page
          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();
          //Locating the REPORTING PERIOD drop down
          WebElement mSelectElement = driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl03_ddValue']"));
          List<WebElement> optionsList = mSelectElement.findElements(By.tagName("option"));
          //Object creation for Fileaccessing class file
          fastack10 fileAccessObject = new fastack10();
          //Reading txt from the Month txt file
          String oldMonth = fileAccessObject.getTheOldMonthFromFile();
          //Message that prints the Old month
          System.out.println("The old month is: " + oldMonth);
          String newMonth ="";


              for (int i = 2; i < optionsList.size(); i++) {
                 //driver.findElements(By.tagName("option")).get(i).click();

                  System.out.println(">>>>>>>>>>>>>>>>>>>>>executing for loop" );
              WebElement element = optionsList.get(i);
              newMonth = element.getText();

              //Message that prints the new month
              System.out.println("The new month is:"+newMonth);





          /*Condition to check if the New month is equal to Old month, if it is not equal then proceeds
           * to download that particular month data or else breaks the loop
           */

          if (oldMonth.equals(newMonth)) {
              System.out.println("No new months are updated to download");
              driver.close();
              break;
          }else if (i==2 & !(oldMonth.equals(newMonth))) {
          //else if (!(oldMonth.equals(newMonth))) {    
          element.click();
                //Click on View Report button
                driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl00']")).click();
                //Wait time to load the selected month data
                Wait(20000);
                //Click on File save button
                driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Button']")).click();
                //wait time to load the File format options
                Wait(20000);
                //Click on csv format button
                driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Menu']/div[2]/a")).click();
                Wait(10000);
                //Success message to indicate that the data is downloaded successfully in csv format
                System.out.println("New Month data downloaded in csv format" +newMonth);
                //Saves the new month txt to the file
                fileAccessObject.saveIntoAFile(newMonth);
                //driver.navigate().back();
                //closes the web page once the file is downloaded
                //driver.close();
                //break;

                //driver.navigate().back();
                //driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");

                Wait(10000);
                driver.navigate().refresh();
                Wait(10000);
                //driver.switchTo().window( mainWindowHandle );


    for(int j=0; j<4;j++)
        try {
            System.out.println(">>>>>>executing 2nd for loop");

            driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl03_ddValue']"));
            //WebElement element = optionsList.get(i);
             element.click();

            break;
           } catch(StaleElementReferenceException e) {
             e.toString();
       System.out.println("Trying to recover from a stale element :" + e.getMessage());

           }  }  } }

/* Wait time is given to continue the next process
 * @param  in int nothing is returned
*/
public static void Wait(int time){

    try {
        Thread.sleep(time);
        } catch (Exception e) {
// TODO: handle exception
} } 

1 个答案:

答案 0 :(得分:0)

执行以下操作:

  1. 创建属性文件并在其上添加键值:

    browser.download.folderList = 2 browser.download.manager.showWhenStarting = FALSE 等

  2. 然后在java加载并读取它(asunming将步骤1中创建的文件命名为config.properties

    Properties prop = new Properties(); InputStream input = null; try {
    input = new FileInputStream("config.properties"); // load a properties file prop.load(input); // get the property value and print it out System.out.println(prop.getProperty("browser.download.folderList")); System.out.println(prop.getProperty("browser.download.manager.showWhenStarting")); ...

  3. 别忘了关闭溪流......