简体   繁体   中英

How to call a method from property file using Java and Selenium WebDriver?

Currently Working on Selenium WebDriver and code I'm writing in Java .

I have created a MasterScript called Master.java which is the main script and it looks like this:

package test;
import org.openqa.selenium.WebDriver;
public class MasterScript {
public static void main(String[] args) throws Exception {

//*****************************************************
//   Calling Methods
//*****************************************************

    LoginOneReports utilObj = new LoginOneReports ();
    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); // instead of calling one PR it need to pick from the property file and it need to select the KPI in UI

    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);
    utilObj2.FilterMatching();
    }   
}

Put this dynamic values in the property file where each and every time it need to go to the property file and fetch the value, based on the value the related java file need to called.

在此处输入图片说明

Hi Just for example we will call the property file as setup.txt

say for example you have a url in ur setup file as "internal.url= https://google.com "

create a constructor

public MasterScript() throws IO Exception
 {
  setup_details();
   }

public void setup_details()throws IOException{

    FileInputStream inStream;
    inStream = new FileInputStream(new File("Setupfiles\\setup.txt"));
    Properties prop = new Properties();
    prop.load(inStream);
    internal_url=prop.getProperty("internal.url");
        }

IN THE SETUP FILE*

internal.url= https://google.com

name the txt file as setup.txt


Now while using that in the main class u can just use "driver.get(internal_url);"

Hope This helps you...

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