场景:从网站下载新的月份报告。
Month.txt:Oct(2015)非常规井
这是结构:
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 get a key value from 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 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:\\"); //need to ask muthu
//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);
}
//To get a url in browser
public static void get_url(String link) throws InterruptedException
{
driver.get(link);
driver.manage().window().maximize();
}
//To select a value by visible text in a 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 from the Month txt file
String oldMonth = fileAccessObject.getTheOldMonthFromFile(propertyfilepath);
System.out.println("The old month is: " + oldMonth);
String newMonth ="";
for (int i = 2; i < optionsList.size(); i++) {
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 available 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(xpath)).click();
//Wait time to load the 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 to load the 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 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
public static void save_new_month() {
fileAccessObject.saveIntoAFile(newMonth, propertyfilepath);
//closes the web page once the file is downloaded
driver.close();
break;
}
} }
public static void Wait(int time){
try {
Thread.sleep(time);
} catch (Exception e) {
// TODO: handle exception
} }
}
我已经成功设置了浏览器初始化和打开,现在如何在download_new_month方法中使用它,因为在满足条件时必须单击3个xpath来下载报告。请帮忙。
答案 0 :(得分:0)
在一个
下面使用的课程开始时 public static WebDriver driver;
再次在public static void initiate_webdriver()方法中,使用
WebDriver driver=new FirefoxDriver(fprofile);
这里看起来不正确,已经全局声明了驱动程序,所以请使用
driver=new FirefoxDriver(fprofile);
在initiate_webdriver()方法中。
谢谢你, 穆拉利