简体   繁体   中英

How to call <li> id values from the property file using java?

Currently I'm working on Selenium WebDriver and Java . I have a master script which runs all the other scripts. The master script as follows:

import org.openqa.selenium.WebDriver;
public class MasterScript {
public static void main(String[] args) throws Exception {
//*****************************************************
//   Calling Methods
//*****************************************************
LoginOneReports utilObj = new LoginOneReports ();// calling my 1st LoginOneReports.java
WebDriver driver;
driver=utilObj.setUp();
if(utilObj.Login()){
System.out.println("Login sucessfully completed");
} else {
System.out.println("Login failed");
System.exit(0);
}
NewPR utilObj1 = new NewPR(driver);// calling my 2st NewPR.java Here I need to change
if(utilObj1.test()){
System.out.println("NewPR KPI page has opened");
} else {
System.out.println("NewPR KPI not able to open");
}
FilterSection utilObj2 =new FilterSection(driver);//calling my 3st Filtersection.java
utilObj2.FilterMatching();
}
}
  1. I have a list of kPI's as follows:

在此处输入图片说明 Currently in my NewPR.java script I'm directly going and clicking on the NewPR The script as follows:

Log.info("Clicking on Overview and Evolution PR link");
if(existsElement("ext-new-prs")==true){
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.id("ext-new-prs") ));
Thread.sleep(6000);
}
else{
Log.info("element not present");
System.out.println("element not present -- so it entered the else loop");
}
return driver.getCurrentUrl().equals("https://10.4.16.159/extranet_newprs/reports/type/default/");
}

Instead of going and clicking each time one KPI. I need to store in a property file where all the list of values in screenshot. Then i need to call any 1 or more KPI and i need to run the 3rd script that is FilterSection.java

My problem is all the KPI HTML are looks like this

<li>
<a id="ext-pr" class="submenu ext-pr" name="ext-pr" href="https://10.4.16.159/reports/">Problem Reports (PR)</a>
<ul>
<li>
<a id="ext-pr-backlog-age" class=" ext-pr-backlog-age" name="ext-pr-backlog-age" href="https://10.4.16.159/extranet_prbacklogage/reports/type/default/">Age</a>
</li>
<li>
<a id="ext-timetoassign-prs" class=" ext-timetoassign-prs" name="ext-timetoassign-prs" href="https://10.4.16.159/extranet_timetoassignprs/reports/type/default/">Average Time To Assign</a>
</li>

I don't have idea how to store these set of values in a property file. could anyone suggest me solution..

Here is a method to load a properties file :

public static Properties loadPropertiesFile(){
    Properties properties = new Properties();
    InputStream input = null;

    try {

        input = new FileInputStream("path_to_your_properties_file");

        // load a properties file
        properties.load(input);

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

If you only provide a file name in path_to_your_properties_file , the file will be located in your project directory. But you can provide a path to a file also I think.

For convenience, use a property file composed of something like that :

total_ids=5
id_1=ext-pr
id_2=ext-pr-backlog-age
id_3=ext-timetoassign-prs
id_4=other_id
id_5=other_id_again

Then after loading your file with the first given method, you can loop through the ids with :

int total = Integer.parseInt(properties.getProperty("total_ids"));
for(int i=1 ; i<= total ; i++){
    String identifier = properties.getProperty("id_"+i);
    WebElement element = driver.findElement(By.id(identifier));
    // Do your tests en 'element'
}

Is it what you want to do ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM