I'm writing my first ever selenium test. It's amazing and I've got it logging into my application which makes me feel giddy. Here's my automated UI stuff using ChromeDriver:
var options = new ChromeOptions();
options.AddArgument("--start-maximized");
IWebDriver driver = new ChromeDriver(options);
Authentication.LogInAsAdmin(driver);
System.Threading.Thread.Sleep(1000);
TenantManagement.CreateTenant(driver, false);
Authentication.LogInAsAdmin works just fine. My single page angular app (upon login) loads additional content and displays it. TenantManagement.CreateTenant(driver, false) is where things go wrong. The first line in that piece is failing:
public static bool CreateTenant(IWebDriver driver, bool testLogin)
{
driver.FindElement(By.Id("TenantsTab")).Click();
...
}
As you can see, I have a thread sleep before this call just to make sure everything is loaded before trying to find the element. When looking at the html in Chrome dev tools (element inspector), I see:
<li ng-class="{active: active, disabled: disabled}" id="TenantsTab" ng-repeat="Tab in SelectedArea.Pages | filter:lessThanTwo" ng-click="trackTab(Tab)" active="Tab.active" heading="Tenants" class="ng-isolate-scope">
<a ng-click="select()" tab-heading-transclude="" class="ng-binding">Tenants</a>
</li>
This element is definately present when the thread wakes back up and tries to find it, but it's not being found. Any clues on what I'm doing wrong here or how one would go about troubleshooting this when using selenium?
The right way to do this seems to be using selenium wait functionality (which I just discovered). By adding this:
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
to the beginning of my CreateTenant() method, selenium waits up to 10 seconds for an element to become available, and adding that worked. Not sure why the thread sleep doesn't.
Related
Below is my code, the line =>
driver.findElement(By.xpath("//*[#id=\"quote_password\"]")).sendKeys("password"); throws exception that element is not found
#Test
public void mytest()
{
System.setProperty("webdriver.chrome.driver","Drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://billing.scriptinglogic.net/index.php/sessions/login");
driver.findElement(By.xpath("//*[#id='email']")).sendKeys("email");
driver.findElement(By.xpath("//*[#id='password']")).sendKeys("password");
driver.findElement(By.xpath("/html/body/div/div/form/input")).click();
driver.findElement(By.xpath("//*[text()='Quotes']")).click();
driver.findElement(By.xpath("//*[text()='Create Quote']")).click();
driver.findElement(By.xpath("//*[#id=\"quote_password\"]")).sendKeys("password");
}
Quick and dirty solution:
WebDriverWait wait = new WebDriverWait(driver, 15, 100);
driver.get("http://billing.scriptinglogic.net/index.php/sessions/login");
driver.findElement(By.id("email")).sendKeys("<EMAIL>");
driver.findElement(By.id("password")).sendKeys("<PASSWORD>");
driver.findElement(By.name("btn_login")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[text()='Quotes']"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".create-quote"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("quote_password"))).sendKeys("password");
Updated based on the credentials supplied in the comment below. I've tweaked the selectors to use ID, name and class where applicable. There is no need to use complex XPath locators when they aren't needed.
The explicit waits are required based on the way the site is working and I've added JeffC's suggestion of using an elementToBeClickable expected condition for the input element (I don't think it's really required in this instance though since the site doesn't seem to ever disable the input field, so a visibilityOfElementLocated expected condition is just as good really).
This solution is working for me in Chrome and Firefox in standard mode and Firefox in headless mode. It's not working in headless mode in Chrome because the screen size is smaller and when the screen width goes below 1000px the header changes and the text "Quotes" is never displayed. Below 767px the header is completely removed and you get a side menu. This means that the flow of the script needs to change slightly based on resolution.
I would suggest asking your developer to add an ID to the menu items, it will make it easier to locate them and use the site in its various states. The quick and dirty solution to this problem is ensure the browser is a certain size while the test runs, you can do this by setting the size in the first line of your script:
driver.manage().window().setSize(new Dimension(1024, 768));
When you do this it passes in Firefox and Chrome in standard and headless mode.
Note: The lines with an explicit wait that result in an element being clicked are anchor elements so there is no point waiting for the element to be clickable as the condition is always going to return true.
I am writing an automated test and want to report bugs, if occur, directly in the repo at GitHub. The step which fails in my program is the Submit new issue button from GitHub Issue Tracker.
Here is the code:
WebElement sendIssue = driver.findElement(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button"));
sendIssue.click();
And the exception:
org.openqa.selenium.WebDriverException: Element is not clickable at
point (883, 547.7999877929688). Other element would receive the click:
div class="modal-backdrop"></div
The following command also does not work:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", sendIssue);
How can I make it clickable? Is there any other way by which I can resolve this issue?
This is happening because when selenium is trying to click ,the desired element is not clickable.
You have to make sure that the Xpath provided by you is absolutely right.If you are sure about the Xpath then try the following
replace
WebElement sendIssue = driver.findElement(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button"));
sendIssue.click();
with
WebElement sendIssue =(WebElement)new WebDriverWait(DRIVER,10).until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button")));
sendIssue.click();
If that doesn't work ,You will get an Timeout exception, In that case try incaresing the timeout amount from 10 to 20.
If it still doesn't work please post a screenshot of the HTML.
You need to write something in the issue title and description to make the issue clickable are you sure you are not making that mistake of clicking the button without writing anything in those places I am adding screenshot for your convenience.
Selenium Webdriver introduced in a previous version (v2.48) a new behavior that prevent clicks on elements that may be overlapped for something else (a fixed header or footer - for example) or may not be at your viewport (visible area of the webpage within the browser window).
You can see the debate here.
To solve this you will need to scroll (up or down) to the element you're trying to click.
One approach would be something like this post:
Page scroll up or down in Selenium WebDriver (Selenium 2) using java
Another, and maybe more reasonable, way to create a issue on Github, would be using their API. Maybe it would be good to check out!
Github API - Issues
Gook luck.
This worked for me. Instead of HTML browser this would be useful if we perform intended Web Browser
// Init chromedriver
String chromeDriverPath = "/Path/To/Chromedriver" ;
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");
WebDriver driver = new ChromeDriver(options);
After implementing filepicker.io, some of our Selenium regression tests have started failing. The failures (intermittent, but more often than not in some circumstances) are that clicks are ignored on WebElements found via XPath queries. e.g.
driver.findElement(By.xpath("//a[text()='Demo data']")).click();
Adding a Sleep(2000) between findElement() and click() generally resolves the problem. (I say generally because Sleep(1000) was mostly enough, until it wasn't, so I made it Sleep(2000)...)
Checking element.isDisplayed() has not helped. The problem disappears if we stop including the filepicker.io JavaScript file.
Is it something to do with filepicker.io introducing an IFRAME? We have also noticed that JQuery's document.ready() seems to be now invoked twice.
As usual with this kind of problems, you are trying to find an element that is not yet available on the page due to AJAX request still downloading/processing it. You need to wait for the element to appear on the page.
There are three ways to do this:
Using sleep(). This is the discouraged way. You should not use hardcoded sleeps, because you'll either wait too long (making the tests unnecessarily slow) or too short (failing the test).
Use Implicit wait. That will always wait for an element if it's not found.
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Use explicit wait. That enables you to wait explicitly for one element to (dis)appear / become available / whatever.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Demo data")));
We now run this code first after opening any page that includes filepicker.js:
while (FindElementsMaybeNone(By.cssSelector("#filepicker_comm_iframe")).size() == 0)
Sleep(50);
while (driver.switchTo().frame("filepicker_comm_iframe") == null)
Sleep(50);
driver.switchTo().defaultContent();
We guess that filepicker's dynamic IFRAME insertion is discombobulating Firefox or Selenium. I'm not marking this as the answer because I don't really know why it works.
I am trying out selenium for the 1st time and I have a quick question. When I call the click() method on a WebElement, I noticed that it is a void type method. So does the HtmlUnitDriver hold the updated page that is rendered after the click() happens?
Yes. The WebDriver interface is controlling the browser, however it's still the browser (in your case, HtmlUnit) that does most of the work and remembers the state of teh page etc.
Therefore, WebDriver as such doesn't really have a state (an overly simplified statement, but true for your purpose). When you send a click() command, it performs it in the browser, than waits for the browser to complete its job (load the new page), then again waits for your commands on the new page.
WebDriver always operates on what the browser currently has.
I can see from your question that you are using HtmlUnitDriver. JavaScript is disabled in this driver by default (for explanation click here). This driver uses the Rhino JavaScript engine and is not used in any of the popular browsers. This might explain why the actions you are trying work fine in Firefox but not in Selenium.
You could try enabling JavaScript in HtmlUnit as follows:
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
But instead I would recommend using FirefoxDriver.
WebDriver driver = new FirefoxDriver();
This should emulate the behavior you're seeing when you navigate through the webpages yourself.
Solution:
Today (2011-04-13) handles Selenium RC the confirmation boxes under Firefox 4 badly.
I had to change back to Firefox 3.16 and then this problem disappears. Thanks again.
Original Question:
Hello Selenium gurus,
I am trying to execute an automated browser test from Java using Selenium 2.0b3 as the Java client and standalone server too.
The server starts a Firefox 4 with a specific profile.
The test is stuck at a "click" command; it waits and no timeout/exception/any error happens. During this click comes up a confirmation box, so I guess that is the problem, but I do not know why this gets stuck. In Selenium IDE comes not this confirmation box, only in RC.
The problem is not that, that the click does not happens (because the confirmation box appears), but that this confirmation box hangs.
I tried these but did not help:
selenium.click("css=div[id=command_Delete]");
selenium.click("id=command_Delete");
String JSscript = "jQuery('#command_Delete').click();" // See http://api.jquery.com/click/
selenium.runScript(JSscript);
These are just ways how to start the click.
Maybe I have to start a different Thread according these link: http://www.sqaforums.com/showflat.php?Cat=0&Number=567974&an=&page=0&vc=1
Thanks: Andras
Java code:
selenium.click("//div[#id='command_Delete']/span");
//stucks here
//so this is not reached:
String confirmation = selenium.getConfirmation();
HTML:
<div id="command_Delete" class="...">
<div>...</div>
<span>Delete</span>
<div>...</div>
<br><br>
</div>
Javascript:
<script type="text/javascript">
$('command_Delete').addEvent('click',function(){
var isConfirmTrue = confirm('Do you want to delete?');
if (isConfirmTrue) {
var myForm = getFormObj(document, "deleteForm");
submitForm(myForm);
}
});
</script>
And in the Java, it hangs waiting the selenium rc to answer:
HttpURLConnection.getInputStream() line: 912 [local variables unavailable]
HttpURLConnection(HttpURLConnection).getResponseCode() line: 367 [local variables unavailable]
HttpCommandProcessor.getResponseCode(HttpURLConnection) line: 147
HttpCommandProcessor.getCommandResponseAsString(String) line: 167
HttpCommandProcessor.executeCommandOnServlet(String) line: 107
HttpCommandProcessor.doCommand(String, String[]) line: 89
DefaultSelenium.click(String) line: 167
...
Have you tried replacing
selenium.click("//div[#id='command_Delete']/span");
with
selenium.click("id=command_Delete");
?
It seems to me that since the event is binded to the div itself that should work.
Additionally, you can execute the click action directly by doing this, I'm using jQuery since you seem to already have it loaded on the page and it makes things easier (especially for cross-browser testing):
String JSscript = "jQuery('#command_Delete').click();" // See http://api.jquery.com/click/
selenium.runScript(JSscript);
You'd just need to evaluate if this solution is good for you.
If a confirmation box is popping up the test will block until that confirmation box has been removed. You can do a quick manual test to see if this is your problem.
Run your test and when the confirmation box comes up manuall y interact with it, if everything else is fine the test should continue from that point as per normal.
Selenium is waiting for the page to load, but the pop up box is blocking the page from loading which is eventually resulting in a timeout.
You can try and work around this using selenium.chooseOkOnNextConfirmation(); before your click.
Are you sure your widget can afford a click?
I would try the CSS identifier:
selenium.click("css=div[id=command_Delete]");