Previously I have been using chrome Auto Refresh plug in. However, now my code has multiple ChromeDriver instances opening and closing and I cannot use Auto Refresh. Also, it is quite a hassle to install Auto Refresh on new computers.
Is there any way to refresh driver (simulate F5 say every 15 seconds if driver does not change remains motionless) with Selenium similar to Google Auto Refresh?
refresh is a built in command.
driver = webdriver.Chrome()
driver.get("http://www.google.com")
driver.refresh()
If you don't have the chrome driver it can be found here:
https://code.google.com/p/chromedriver/downloads/list
Put the binary in the same folder as the python script you're writing. (Or add it to the path or whatever, more information here: https://code.google.com/p/selenium/wiki/ChromeDriver)
edit:
If you want to refresh ever 10 seconds or something, just wrap the refresh line with a loop and a delay. For example:
import time
while(True):
driver.refresh()
time.sleep(refresh_time_in_seconds)
If you only want to refresh if the page hasn't changed in the meantime, keep track of the page that you're on. driver.current_url is the url of the current page. So putting it all together it would be:
import time
refresh_time_in_seconds = 15
driver = webdriver.Chrome()
driver.get("http://www.google.com")
url = driver.current_url
while(True):
if url == driver.current_url:
driver.refresh()
url = driver.current_url
time.sleep(refresh_time_in_seconds)
Well there are two ways of doing this.
1. We can use refresh method
driver.get("some website url");
driver.navigate().refresh();
We can use actions class and mimic F5 press
Actions act = new Actions(driver);
act.SendKeys(Keys.F5).perform();
If you write unit tests that must be run like if you had to open/refresh a new browser session each time, you can use a method with before annotations:
#Before
public void refreshPage() {
driver.navigate().refresh();
}
If all tests are individually successful (green) but fail all together, the reason might also been that you need to wait for some resources to be available on the page, so you also need to handle it, setting the timeout like this:
public WebElement getSaveButton() {
return findDynamicElementByXPath(By.xpath("//*[#id=\"form:btnSave\"]"), 320);
}
320 is a long time, but you must make sure that you give enough time to get all that it takes to test.
Related
I am writing automation tests that send keys to every text box on a form (about 5 fields) and then a submit button becomes enabled and clicked. I find that the test frequently fails as only the last field is populated and the button never becomes enabled.
It seems like the method to send keys may be executing too fast for the page to be populated. So far I have attempted to click on each field before sending keys (as well as waiting for the elements to exist) and this doesn't seem to help.
I have also tried to verify the page is fully loaded by waiting for every element and button to exist on the page before proceeding.
The browser I am testing against is chrome. (Version 79.0.3945.130)
The selenium web-driver is 3.11.2
The chrome driver is up to date with chrome
private IWebElement FirstNameInput => Webdriver.FindElement(By.Id("first-name-input"));
// The remaining input fields
public void VerifyPageIsFullyLoaded()
{
// Wait until all elements exist
}
public void EnterFormDetails(FormDetail formDetail)
{
WebDriver.WaitUntilElementExists(FirstNameInput);
FirstNameInput.Click();
FirstNameInput.SendKeys(formDetail.FirstName);
WebDriver.WaitUntilElementExists(LastNameInput);
LastNameInput.Click();
LastNameInput.SendKeys(formDetail.LastName);
WebDriver.WaitUntilElementExists(DateOfBirthInput);
DateOfBirthInput.Click();
DateOfBirthInput.SendKeys(formDetail.DateOfBirth);
WebDriver.WaitUntilElementIsClickable(SubmitButton);
SubmitButton.Click();
}
Update:
Just tried the latest stable release of selenium web driver (3.141.0) and found that it is still not as reliable.
JavascriptExecutor javascript = (JavascriptExecutor)webdriver;
javascript.executeScript("document.getElementById('FirstNameInput').value='test123'");
javascript.executeScript("document.getElementById('LastNameInput').value='test123'");
javascript.executeScript("document.getElementById('DateOfBirthInput').value='test123'");
SubmitButton.Click();
and if the submit button is in form tag then you can use submit() method.
I'm trying to validate if selenium-chromedriver can share cookies between multiple webdriver instances. The idea is that, I'll create one webdriver instance and login into the application. I'll keep this webdriver instance running and will create another webdriver instance and try to access a secure page on the same site. Since I already logged into the application from first instance, I should be automatically logged into the second instance. But this didn't work. After a lot of research, I found that I need to set a specify the directory where Chrome creates the session cookies while creating the chromedriver instance. Following is the code I have.
public class TestClass {
private static WebDriver webDriver = null;
public static void main(String[] args) throws InterruptedException {
TestClass tc = new TestClass();
if(webDriver == null) {
webDriver = tc.getWebDriverInstance();
webDriver.get("https://example.com/loginpage");
//enter userid/password, click login button
//login is successful and redirected to next page - https://example.com/securepage
}
WebDriver newWebDriverOne = this.getWebDriverInstance();
newWebDriverOne.get("https://example.com/securepage"); // this doesn't work
WebDriver newWebDriverTwo = this.getWebDriverInstance();
newWebDriverTwo.get("https://example.com/securepage");// this doesn't work
}
WebDriver getWebDriverInstance(){
DesiredCapabilities dCaps = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/user/me/selenium/chrome");
dCaps.setCapability(ChromeOptions.CAPABILITY, options);
return new ChromeDriver(dCaps);
}
}
Problem with this is, when I call get() on the newWebDriver instance which is created after the first one, nothing happens. Selenium opens the second window as since I created a new webdriver instance, but get() doesn't do anything. I tried manually entering the securepage url in the opened window and it worked. I was able to see the secure page without getting redirected to login page.
It seems like it is impossible to have multiple webdriver instances if we specify user-data-dir. Is there any other option to share cookie data between sessions?
----- Update ------
The reason I'm trying to do this is a very peculiar usecase.
--> I need to run multiple automation runs simultaneously - only way I know to achieve this is creating multiple webdriver instances.
--> Automation script needs to login using only one account. And the IDP allows only one active session at a time. That means, if the automation script logs in second webdriver instance, then the first webdriver instance logs out.
So while researching a way to share session between webdriver instances, I came across user-data-dir option.
Couldn't you just use one Chrome Profile and have the cookies saved on the one profile and it would automatically login with those details as it remembers you? I don't see why you would need two instances.
If you wanted to use multiple instances I assume you'd have to load up the exact same profile in a separate instance as a new profile would not work.
How to do the Automation testing of any Google map. I have a map in my project/application, now I want to click on each markers.
Since you have no experience with WebDriver, I'm going to give you the answer you want (and not the one you need which is "Go look at WebDriver manual and tutorials.").
Java example:
// opens up Chrome, but you can use any other browser
WebDriver driver = new ChromeDriver();
// goes to GMaps page and searches for "Washington"
driver.get("https://maps.google.com/maps?q=Washington");
// clicks the only marker on the page
driver.findElement(By.cssSelector("img[src*='markerTransparent.png']")).click();
// don't forget to kill the browser or else you'll have neverending chromedriver.exe processes
driver.quit();
Now, you need to take a step back, look at WebDriver, choose a language in which you want to write your tests, go through the API and some examples, then try to implement your tests and if something goes astray, feel free to post another question with a particular issue (just make sure to search for it first).
You can click on each marker by locating that marker using ID.
Here is one script which I wrote to click on marker of google maps
d = Selenium::WebDriver.for :firefox
d.get 'http://maps.google.com'
d.find_element(:id, 'gbqfq').click
d.find_element(:id, 'gbqfq').send_keys 'hdfc bank pune'
d.find_element(:id, 'gbqfb').click
d.find_element(:id, 'mtgt_J.1000').click
I understand that the top level webdriver API doesn't provide access to the page load event handler, but is there any way (including sending a command directly to the server through the REST interface) to make a call directly to the selenium server to force the page load block behavior? I know about the wait for element hack, but I'd rather go straight to the source if at all possible. The specific problem I'm having is a page that makes a JS call when a button is clicked that displays a modal dialog on the page while some backend processes happen, then forwards the browser to a new page once the backend work is complete. Since the click action doesn't directly trigger the new page, selenium doesn't block on the event (and I wouldn't want it to in all cases anyway).
I've looked through the Command class for any promising looking commands, but didn't see anything. I found http://code.google.com/p/selenium/wiki/JsonWireProtocol but it didn't help either...
Thats a tricky one.
Accessing the http status in selenium/webdriver is not very handy.
I would recommend a pragmatic way. IMO the wait for element approach is not a hack, its the proper way to do it. In your case I would wait for selenium.getLocation() or webdriver.getCurrentUrl() contains an expected value.
Something like this:
webDriverWait.until(new Function<WebDriver, WebElement>() {
#Override
public WebElement apply(WebDriver driver) {
//TODO: pass expected url as parameter
boolean expectedUrl = driver.getCurrentUrl().contains("your redirect url");
if (expectedUrl) {
//If not null is returned, we have found something and waitUntil stops
return new RemoteWebElement();
}
//try again
return null;
}
});
I just started using Selenium Web Driver to test an online banking transaction application.
I love it, but there is something that annoy me. Let say i access the login screen with this code:
driver.get("https://webdev.myurl:18113/");
WebElement element = driver.findElement(By.name("username"));
element.sendKeys("xxxx");
element.submit();
the browser start and the page load and display. But it look like the page try loading element from an external site and the findElement (2nd line) wait for these request to complete!
Is there a way to bypass this beahvior?
I tried this too :
WebElement element = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>() {
#Override
public WebElement apply(WebDriver d) {
return d.findElement(By.name("username"));
}
});
But it does not help since this line seems to execute only when the page is totally loaded.
EDIT: I spoke with one of the guy here.. and he told me ipinvite.iperceptions.com is not called by our app.!!! and in fact when i load the site in FF, i don't see this call?!
Does Selenium web driver call this site : ipinvite.iperceptions.com?
Anyone have the same issue?
You can try setting implicitly wait time and page load time to 0. Google "selenium implicitly wait time" and "selenium page load time."
Time outs on get function have not been implemented yet.
When creating a new FirefoxDriver, there are overloads in the constructor that allow you to specify a command timeout which is the maximum time to wait for each command.
You could refer to the answer on this post
ok, i found the problem. I commented out the setPreference to my FirefoxProfile that was setting the proxy parameters. I noticed i did not need them anyway. And now there is no more call to this wierd ipinvite.iperception.com!
Thanks for the time you took to reply
Regards