selenium - How to not Hard code the program in java? Used property file. But unable to use it with multiple xpaths? -


scenario: download new month report site.

month.txt: oct(2015) unconventional wells

here structure:

config.properties:

#browser browser=http://www.depreportingservices.state.pa.us/reportserver/pages/reportviewer.aspx?%2foil_gas%2foil_gas_well_historical_production_report #drop-down box list_box= .//*[@id='reportviewercontrol_ctl04_ctl03_ddvalue'] #view report button view_report = .//*[@id='reportviewercontrol_ctl04_ctl00'] #save button save_button = .//*[@id='reportviewercontrol_ctl05_ctl04_ctl00_button'] #csv button csv_button = .//*[@id='reportviewercontrol_ctl05_ctl04_ctl00_menu']/div[2]/a #read path read_path = e:\\ashik\\wkspselenium\\propertypractice\\src\\package1\\month.txt #write path write_path = e:\\ashik\\wkspselenium\\propertypractice\\src\\package1\\month.txt 

implementation.java:

package package1;  import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.util.list; import java.util.properties;  import org.openqa.selenium.by; import org.openqa.selenium.webdriver; import org.openqa.selenium.webelement; import org.openqa.selenium.firefox.firefoxdriver; import org.openqa.selenium.firefox.firefoxprofile;  import package1.fileaccessing;  public class implementaion {      public static  webdriver driver;     public  fileinputstream fis;     public static string propertyfilepath="config.properties";     public string browsername;      //to key value property file     public string getproperty(string key) throws ioexception, filenotfoundexception{         fis=new fileinputstream(propertyfilepath);         properties prop=new properties();         prop.load(fis);         return prop.getproperty(key);      }     public  static void initiate_webdriver() throws ioexception     {             // changing default file downloading location  path using 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:\\"); //need ask muthu           //csv format download data           fprofile.setpreference("browser.helperapps.neverask.savetodisk", "text/csv");           //automatically downloads 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 created profile firefox driver            webdriver driver=new firefoxdriver(fprofile);     }      //to url in browser         public  static void get_url(string link) throws interruptedexception         {               driver.get(link);             driver.manage().window().maximize();         }          //to select value visible text in drop down            public static void downloads_new_month(string xpath, string value)  throws interruptedexception          {               webelement mselectelement = driver.findelement(by.xpath(xpath));               list<webelement> optionslist = mselectelement.findelements(by.tagname("option"));               fileaccessing fileaccessobject = new fileaccessing();               //reading txt month txt file               string oldmonth = fileaccessobject.gettheoldmonthfromfile(propertyfilepath);               system.out.println("the old month is: " + oldmonth);               string newmonth ="";                 (int = 2; < optionslist.size(); i++) {                webelement element = optionslist.get(i);               newmonth = element.gettext();                //message prints new month               system.out.println("the new month is:"+newmonth);                 /*condition check if new month equal old month, if not equal proceeds                * download particular month data or else breaks loop                */                if (oldmonth.equals(newmonth)) {               system.out.println("no new months available 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(xpath)).click();                 //wait time load selected month data               wait(20000);}                public static   savebutton(string xpath2 , string value2)   throws interruptedexception{                 //click on file save button                 driver.findelement(by.xpath(xpath2)).click();                 //wait time load file format options               wait(20000); }           public static void csvbutton(string xpath3 , string value3) throws interruptedexception{                 //click on csv format button               driver.findelement(by.xpath(xpath3)).click();               wait(10000);                 //success message indicate data downloaded in csv format               system.out.println("new month data downloaded in csv format:" + newmonth);          }               //saves new month txt file          public static void save_new_month() {               fileaccessobject.saveintoafile(newmonth, propertyfilepath);               //closes web page once file downloaded               driver.close();               break;          }         } }             public static void wait(int time){              try {                 thread.sleep(time);                 } catch (exception e) {         // todo: handle exception         } }  } 

i have set browser initialization , opening, how use in download_new_month method, there 3 xpath's has clicked download report when condition met. please help.

at starting of class used below one

  public static  webdriver driver; 

again in public static void initiate_webdriver() method, using

 webdriver driver=new firefoxdriver(fprofile); 

looks not correct here, driver declared globally, please use

 driver=new firefoxdriver(fprofile); 

in initiate_webdriver() method.

thank you, murali


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -