To get the content from .properties file into selenium framework - java

# Compulsory Dimension to create port
xOffset=-3
yOffset=50
How to get these xOffset and YOffset in java file.I tried with inputstream but not getting.These variable should get loaded in java file.

You can use Properties class from Java library
Properties prop = new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
prop.load(inputStream);
The you can get the values as
prop.getProperty("propertyname");

Try the following code:
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("your_config.properties");
prop.load(input);
System.out.println(prop.getProperty("xOffset"));
System.out.println(prop.getProperty("yOffset"));
} catch (IOException e) {
// ...
}

As above explained Create a Function to read the property File During or Before Selenium Driver Constructor . So you can use them in test ( Help in Desire Capability Impl).
Store the values in Public Static final ( If you don not want to change them in Selenium and use as Default Property input)
As the values are read by Java Propertie file or .config file before selenium Driver so you can use them in Driver constructor or if you don't want you can use those properties stored as Static anywhere in the project. These values act as GLOBAL param.

Related

reuse parameters defined within config.parmeters file in java

I have a config properties file in a java project created in eclipse.
Below are the contents in the properties file.
adminApp= testAdminDemo
customerApp = testCustDemo
appHostIP = 172.22.XX.XX
adminAppURL = http://appHostIP:9049/adminApp
customerAppURL = http://appHostIP:9049/customerApp
When the appURL parameter is read from a Java class,
Properties prop = new Properties();
FileInputStream f = new FileInputStream(System.getProperty("user.dir") + "\\config.properties");
prop.load(f);
String url = prop.getProperty("appURL");
System.out.println("URL: " + url);
the output is the same value mentioned for parameter 'url' in config.parameters file:
http://appHostIP:9049/adminApp
Actually, I expected the output to be like:
http://172.22.XX.XX:9049/testAdminDemo
Is there anything wrong in this approach?
I don't want to read host ip and app name in the java class file and then form a string. Instead, the required URl should get formed in the properties file as need to deal with different apps - admin, customer, etc.
You cannot perform concatination in the property file, You have to handle the concatination in code where you are using it like below.
appHostIP = 172.22.XX.XX
adminAppURL = :9049/adminApp
String url = "http://" + "prop.getProperty("appHostIP")+ prop.getProperty("appURL");

Auto-Detect File Extension with APACHE JENA

I want to convert any file extension to .ttl (TURTLE) and I need to use Apache Jena, I am aware of how it can be accomplished using RDFJ4 but the output isn't as accurate as it is using Jena. I want to know how I can auto-detect the extension or rather file type if I am not aware of the extension when reading a file from a directory. This is my code when I hardcode the file-name, it works, I just need help in auto detecting the file type. My code is as follows:
public class Converter {
public static void main(String[] args) throws FileNotFoundException {
String fileName = "./abc.rdf";
Model model = ModelFactory.createDefaultModel();
//I know this is how it is done with RDF4J but I need to use Apache Jena.
/* RDFParser rdfParser = Rio.createParser(Rio.getWriterFormatForFileName(fileName).orElse(RDFFormat.RDFXML));
RDFWriter rdfWriter = Rio.createWriter(RDFFormat.TURTLE,
new FileOutputStream("./"+stripExtension(fileName)+".ttl"));*/
InputStream is = FileManager.get().open(fileName);
if (is != null) {
model.read(is, null, "RDF/XML");
model.write(new FileOutputStream("./converted.ttl"), "TURTLE");
} else {
System.err.println("cannot read " + fileName);
}
}
}
All help and advice will be highly appreciated.
There is functionality that handles reading from a file using the extension to determine the syntax:
RDFDataMgr.read(model, fileName);
It also handles compressed files e.g. "file.ttl.gz".
There is a registry of languages:
RDFLanguages.fileExtToLang(...)
RDFLanguages.filenameToLang(...)
For more control see RDFParser:
RDFParser.create().
source(FileName)
... many options including forcing the language ...
.parse(model);
https://jena.apache.org/documentation/io/rdf-input.html

updating the value read using value annotation in spring

I have a properties file say as follows:
apple=1
mango=2
banana=3
pineapple=4
I am using value annotation in the java program to access the values. I have a method in my class that computes a value i want to update the apple attribute in the properties file with the value that the method returns.
public class test {
#Value("${apple}")
private int apple;
public void testMethod() {
int new_val = 0;
if (apple > 0)
new_val = 300;
else
new_val = 200;
// now i want to update the value of apple in the file to new_val,(apple = new_val) other attributes should remain unchanged.
}
}
can someone let me know how to update the value in the properties file. In this example i want my properties file to become
apple=300
mango=2
banana=3
pineapple=4
Usually we defines constant values in properties, so it does not change.
But if it is your requirement to change it.
You can do it like:
1) Using Apache Commons Configuration library
PropertiesConfiguration conf = new PropertiesConfiguration("yourproperty.properties");
props.setProperty("apple", "300");
conf.save();
2) Using Java input and output stream
FileInputStream in = new FileInputStream("yourproperty.properties");
Properties props = new Properties();
props.load(in);
in.close();
FileOutputStream out = new FileOutputStream("yourproperty.properties");
props.setProperty("apple", "300");
props.store(out, null);
out.close();

trying to pass fileinputstream obj from function into another function

I would like to use a function to read from a properties file and than use the object of that file at various places.here is what I have tried so far with no luck.
public class all_the_functions
{
public FileInputStream loadPropertiesFile(FileInputStream obj2) throws IOException
{
//reading from the properties file
Properties obj = new Properties();
FileInputStream fileobj = new FileInputStream("//Users//macuser//Desktop//selenium//project_Mat//input_properties.properties");
obj.load(fileobj);
return fileobj;
}
}
And than in my main function I am using the following code
public class searchdynamic();
{
FileInputStream Obj;
all_the_functions func = new all_the_functions();
func.loadPropertiesFile(Obj);
WebDriver driver = new FirefoxDriver();
driver.navigate().to(Obj.getproperty("valid URL");
}
The end goal is to read from. The input file by using the function and stroung the properties file so that I can always call the same function when I need to read from the file. Can someone please point me to what I am doing wrong here.
Thanks.
Can u try this
public class searchdynamic();
{
FileInputStream Obj;
all_the_functions func = new all_the_functions();
Obj=func.loadPropertiesFile(Obj);
WebDriver driver = new FirefoxDriver();
driver.navigate().to(Obj.getproperty("valid URL");
}
What is the string that your property is returning? "http://www..."
In theory, this should work (although I'm used to webdriver being navigate().GoToUrl(string url). Give that a shot...
Also, friendly warning about this being not oop or compiling. Might be a good idea to tie your properties to values somehow. I understand this is a keyword driven framework, but it is better practice to have a user input "Google" and have your code digest that into something like
driver.Navigate().GoToUrl("http://google.com"); // (c#)
This way, you can count on correct input and handle incorrect input appropriately.
Regardless, try .Navigate().GoToUrl()

read xml data in selenium

I want to read data from an XML file. I am using Java & Selenium WebDriver. I have found many solutions while researching. The problem is none seems to apply to my problem.
My XML file is as such :
<Enviroment>
<Parameter>Test_Url</Parameter>
<value>https://www.google.com</value>
<Parameter>Distributed_Test</Parameter>
<value>no</value>
<Parameter>Result_Name</Parameter>
<value>Google_Results</value>
</Enviroment>
The code I am using to read this xml file is here.
public class ReadXML {
static String value;
public static void main(String[] args) throws FileNotFoundException,IOException {
File file = new File("path of the file");
FileInputStream fileInput =new FileInputStream(file);
Properties prop =new Properties();
//prop.load(fileInput);
prop.loadFromXML(fileInput);
fileInput.close();
Enumeration enumKeys=prop.keys();
while(enumKeys.hasMoreElements()){
//String node = "Environment";
String subnode= "Parameter";
if(((String) enumKeys.nextElement()).contains(subnode)){
value = prop.getProperty(subnode);
System.out.println(value);
}
}
return ;
}
When I am using prop.load(fileInput), output is printed as null thrice for the three parameter values, I believe.
But if I use prop.loadFromXML(fileInput), InvalidPropertiesFormatException is shown.
Please help..Thanks in Advance!!
The error in the properties format can be checked via xmllint:
xmllint foo.properties
In this case, the closing tag is:
</Enviroment>
but needs to be:
</Environment>
References
Class Properties

Categories