In this post, we will see How to Read Data from Properties File in Selenium Webdriver | Read Data from Properties File in Java
Program Code (data.properties) :
browser=edge
url=https://www.irctc.co.in/nget/train-search
Program Code (PropertiesExample.java) :
package main;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PropertiesExample {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("C:\\Users\\parag\\eclipse-workspace\\Selenium8\\src\\mydata\\data.properties");
Properties p=new Properties();
p.load(fis);
String mybrowser=p.getProperty("browser");
String myurl=p.getProperty("url");
System.out.println(mybrowser);
if(mybrowser.equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get(myurl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
}
else if(mybrowser.equals("firefox"))
{
System.setProperty("webdriver.gecko.driver", "C:\\selenium\\geckodriver-v0.31.0-win64\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get(myurl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
}
else if(mybrowser.equals("edge"))
{
System.setProperty("webdriver.edge.driver", "C:\\selenium\\edgedriver_win64\\msedgedriver.exe");
WebDriver driver=new EdgeDriver();
driver.get(myurl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
}
}
}
Watch on YouTube: https://www.youtube.com/watch?v=I2uuBh2F6zw
No comments:
Post a Comment