Selenium - echo Base URL - selenium

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)

Related

Send a numpad key (with Num Lock off)

I want to test how my app reacts to numpad keys. I found in https://www.w3.org/TR/webdriver/ specs that for example for Numpad Home (with location = DOM_KEY_LOCATION_NUMPAD = 3) a symbol \uE057 should be used. However, it doesn't work for me: I get Home with default location (0), moreover, event.code is empty. It gives me a different result when I physically press NUMPAD7 button with Num Lock off: it that case, I get correct location 3 and event.code is Numpad7.
var options = FirefoxOptions();
options.setLogLevel(FirefoxDriverLogLevel.TRACE);
var driver = FirefoxDriver(options);
driver.navigate().to("https://keycode.info/");
driver.findElementByTagName("body").sendKeys("\uE057");
So how can I send such a key? I'm now thinking of manual recording of generated events when I physically press a key, and then sending these events via Selenium's execution of JS script. However, I haven't tried it yet; maybe there is a better way to do it in Selenium; maybe there is another framework that allows it better.
By the way, I've filed a similar ticket in geckodriver because it looks like a bug of webdriver to me...
\ue01d is the unicode for NUmberpad3
python code:
from selenium import webdriver
from selenium import webdriver
options = webdriver.ChromeOptions()
driver = webdriver.Chrome()
driver.get("https://keycode.info/")
driver.find_element_by_tag_name("body").send_keys("\ue01d")
input()
you can use https://www.javadoc.io/doc/org.seleniumhq.selenium/selenium-api/latest/org/openqa/selenium/Keys.html to send keys instead of sending unicode directly
In firefox this is will work if you use action chain:
ActionChains.send_keys("\ue01d").perform()

Can I Automated Speed Test using Selenium?

I am using beta.speedtest.net to check my network speed. I want to automate this process using Selenium(open to other frameworks as well). This automation process consists of few steps
Clicking on Change Server link
Type my preferred server location
Choose the server of my choice
Run the test
Get the result of each test (in any way possible)
How I am proceeding
public void RunTheTest()
{
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("http://beta.speedtest.net");
driver.FindElement(By.LinkText("Change Server")).Click();
}
Unable to find any element that is critical for my process.
The LinkText won't work in your case because the text contains leading and trailing white spaces. Those may be Non-Breaking SPaces
So you have to go with xpath with contains or normalize-space
//a[normalize-space(text())='Change Server']
//a[contains(text(),'Change Server')]
If you want simple then go with className
btn-server-select
as there is only one element with such className
Try this:
driver.findElement(By.xpath("//a[#class='btn-server-select' and contains(text(), 'Change Server')]")).click();

JSONOBject hash not found

I am working on Test Automation Script using JAVA and Selenium WebDriver ,
My test is running on cloud environment (crossbrowsertesting.com).
There is an feature to take snapshots of browser window ,
When I was using RemoteWebDriver this line of code work fine , but need to replace it with WebDriver because reason not bale to get windowHandles.
But I am getting following error now , stating
"The method getSessionId() is undefined for the type WebDriver"
snapshotHash=myTest.takeSnapshot(driver.getSessionId().toString());
//takeSnapshot method :
public String takeSnapshot(String seleniumTestId) throws UnirestException {
System.out.println("Screen Shots Taken.");
/*
* Takes a snapshot of the screen for the specified test.
* The output of this function can be used as a parameter for setDescription()
*/
HttpResponse<JsonNode> response = Unirest.post("http://crossbrowsertesting.com/api/v3/selenium/{seleniumTestId}/snapshots")
.basicAuth(username, api_key)
.routeParam("seleniumTestId", seleniumTestId)
.asJson();
// grab out the snapshot "hash" from the response
snapshotHash = (String) response.getBody().getObject().get("hash");
return snapshotHash;
}
I dont quite understand why you need to use "WebDriver" in place of "RemoteWebDriver" ? "RemoteWebDriver" is the mother of all web driver implementations and it should be good enough to work with any remote grid environment. I dont understand why you need to switch to using "WebDriver" reference which is one of the interfaces that "RemoteWebDriver" implements. getSessionId() is NOT part of any interface specifications but its a direct implementation that RemoteWebDriver provides.
getWindowHandles() is part of WebDriver interface specification and you should still be able to use it.

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

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")

Selenium's WebDriver.execute_script() returns 'None'

My program is having trouble getting an existing class from a webpage using Selenium. It seems that my WebDriver.execute_script function is not working.
import urllib
from selenium import webdriver
#Path to the chromedriver is definitely working fine.
path_to_chromedriver = 'C:\Users\Ben\Desktop\Coding\FreeFoodFinder\chromedriver.exe'
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'http://www.maidservicetexas.com/'
browser.implicitly_wait(30)
browser.get(url)
content = browser.execute_script("document.getElementsByClassName('content')");
#Just printing the first character of the returned content's toString for now. Don't want the whole thing yet.
#Only ever prints 'N', the first letter of 'None'...so obviously it isn't finding the jsgenerated content even after waiting.
print content
My program returns 'None,' which tells me that the javascript function is not returning a value/being executed. Chrome's web dev tools tell me that 'content' is certainly a valid class name. The webpage isn't even dynamically generated (my eventual goal is to scrape dynamic content, which is why I make my WebDriver wait for 30 seconds before running the script.)
Return the value:
content = browser.execute_script("return document.getElementsByClassName('content');");