how to Pass command line argument ( baseURL) for selenium WebDriver - selenium

Kindly help.
i have created a runnable jar for my Selenium webDriver suite. now i have to test this in multiple environment( QA , Demo box, Dev ). But my manager doesnt what it to be hard coded like below
driver.get(baseUrl)
As the baseURl will change according to the need. My script is given to the build team. So all they will do in the command prompt is
java -jar myproject-1.0.1.jar
So my manager has asked me to send the baseUrl as a command line argument so that build team do not have to go to my script and manually change the baseUrl. They should be able to change the URL every time they run the script from the command line itself. Something like this
java -jar myproject-1.0.1.jar "http://10.68.14.248:8080/BDA/homePage.html"
Can somebody please guide me through this. Is it possible to send command line arguments to Selenium Web Driver driver.get(baseUrl)
Thanks in advance

From your question above I recon you want pass URL at runtime, means your URL changes time to time so beside hardcoded URL , you want pass at the time your automation code runs. So, let me give you 2 simple solutions.
You can send URL dynamically or at Run time by using javascript executor:
try{
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("var pr=prompt('Enter your URL please:',''); alert(pr);");
Thread.sleep(15000L);
String URL = driver.switchTo().alert().getText();
driver.switchTo().alert().accept();
driver.get(URL);
}catch(Throwable e)
{
System.out.println("failed");
}
Use this code in place of driver.get(); , so that a prompt box will appear when you run your code and within 15 secs or it will throw a error(you can change the time in Thread.Sleep) you will give the current Valid URL and hit Enter, the navigation will go to the URL. So that you can use different URL for same set of testscripts.
By using Scanner Class:
String url = "";
System.out.println("Enter the URL :");
Scanner s = new Scanner(System.in);
url = s.next();
s.close();
By using this you can give needed URL in your Console (If you are using Eclipse).
I recommend try Javascript excutor in your code and then create a runnable jar file and just run the Jar file, you will know and it will be the better solution than commandline URL passing . Let me know :)

Another way is to supply arguments this way -Durl=foobar.com and extract them in runtime like this
String URL= System.getProperty("url")

Related

Protractor get console variable value

In Firefox browser developer tool console, I type var a = libraryform.firstname
It returns the firstname value entered by user for that form.
I am new to protractor and selenium. How can I call library.firstname in protractor to get the value of fistname?
Use Javascript Executor to execute the javascript which you able to get them working in developer console against your application.
For e.g.,
this returns web-element which retrieved through javascript executed in browser.
var element = browser.executeScript("document.getElementById('identifier1')");
Refer this page for more examples
If this single line returns required value in browser console, then the protractor code should be some thing like this,
var name = browser.executeScript('return libraryform.firstname')
If you need too many lines of js, you can specify with semicolon separated,
var name = browser.executeScript("var element=$('.dropdown-toggle').eq(0); return element.text();")

To Take Screenshot of a particular page using cucumber and JAVA8

I was trying to take the screenshot of a particular output screen for all the tests.The URL of the page differs for each test depending on the environment (QA,DEV) and also the reference number created.
For example "https://xyz-QA-abc.com/ABCDEF/123456"
Here the QA can be changed and 123456 is different for each test.I am doing my work in cucumber using JAVA8.I am not using selenium webdriver.I tried with the code below in HOOKS.But it is not working.It is showing error in browser,attach,buffer,base64png .Could someone help me with a better code
if(scenario.isFailed()){
return browser.takeScreenshot()
.then((base64png)=>{
scenario.attach(new Buffer(base64png,'base64'),'image/png');
});
Try this:
byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");

Selenium IDE Test Case Pass / Fail

I am new in using Selenium IDE. I'm writing this test case where User A clicks this link and then it should direct the user to the correct page. Unfortunately, the page returns:
An error occured. Message: script 'pp/agensi-list.phtml' not found in path (C:/htdffocs/star/application/views\scripts/**)
But on my selenium, it shows the test case has passed (it should fail).
Can someone tell me why?
Hi it is very easy to write test case results pass/fail using selenium with testng framework,please read from below link i have given clear examples.
How can I write Test Result (Pass/Fail) in Excel file using TestNG Framework with Selenium WebDriver?
For more with real time examples ,you can read write test case pass/fail in excel using testng
Simply you have to create Excel utility for fileinput stream and fileoutputstream classes as below and call that class into another classes using extends keyword
Excel Utility:
public class Excelutility {
#BeforeTest
public void exceloperation(){
FileinputStream file = new FileiinputStream("file path");
//do file input stream details here
Fileoutputstream newfile=new Fileoutputstream("filepath");
//do fileoutputstream details here like create workbook,create sheets etc
}
}
Another class:
public class operations{
#Test
public void openbrowser(){
//write driver operations here
//write excel operations using
if(Title.equalsIgnoreCase("HP Loadrunner Tutorial")){
Testcase="PASS";
}else{
Testcase = "FAIL";
}
Label l1=new Label(1,2,"status")
writablesh.addCell(l3);
}
The test is likely passing because you aren't verifying anything after the click.
If the test stops after the link is clicked then it will pass. You have to confirm that something happened. Look for a change in the CSS or perhaps even look for the message you listed above?

Integrating RFT Test framework to work with RQM

I designed a framework in RFT where the test cases are written in spreadsheet specifying the data source, object and keyword and a driver script which processes through all this data and routes it to the appropriate method for each test step all in a spreadsheet. Now I want to integrate this with RQM so that each of my test cases in the spreadsheet is shown as passed/failed in RQM. Any ideas?
You could implement now an algorithm to read those testcases in the spreadsheet and pass them to RQM as attachments with logTestResult.
For example:
logTestResult( <your attachment> , true );
And if you are already connected to RQM the adapter will attach files that you indicate automatically to RQM. So, at the end you will see step by step the results and if the script ends correctly RQM will show you the script as "passed".
Thanks for the answer Juan. I solved this by passing the testcase name from Script Argument part of RQM and fetching the arguments in my starter script as shown below:-
public void testMain(Object[] args) throws Exception
{
String n=args[0].toString();
logInfo("Parameter from RQM"+n);
ModuleDriver d=new ModuleDriver();
d.execute_main(n);
}
Since I have verification points setup for each of the steps in my test cases the results get reported based on each of those verification points in RQM which is what i needed.

Selenium - echo Base URL

How can I read the current value of Base URL in Selenium IDE 2.8.0?
Please suggest a working version of following selenese:
echo ${BASEURL}
Take your instance of the IWebDriver and put .Url after it. This gets the current URL that the driver is associated with. Then you can use whatever output mechanism you want to use. So if you go with the echo...
echo ${driver.URL}
where driver equals your active selenium WebDriver instance. If you want just the root of the URL then you need to do a regex expression on the returned URL and look for the .com/.net/.org and chop anything after that off.
If you are using php you might want to look here: http://forums.phpfreaks.com/topic/175838-extract-base-url-from-entire-url/
1) Create IWebDriver instance
IwebDriver driver = new FireFoxDriver();
2) navigate to URL
driver.navigate().to("");
3) Print the URL
printline("The base URL is " + driver.URL);
(please ignore syntactical and language specific errors)