Webdriver Backed Selenium, missing confirmation dialog - java

I have a problem related to some strange behavior when trying to test confirmation popups using Webdriver Backed Selenium in JUnit.
I have a following chunk of code in my test case:
assertTrue(isElementPresent(By.xpath("//input[#title='Delete Site']")));
selenium().click("//input[#title='Delete Site']");
//assertTrue(selenium().isConfirmationPresent());
//assertEquals(selenium().getConfirmation(), "Are you sure want to delete this item?");
selenium().waitForPageToLoad("30000");
The above lines refer to the following element:
<input onclick="return confirm('Are you sure want to delete this item?');" value="Delete Site" type="image" title="Delete Site" src="/workthru-web/resources/images/delete.png" class="image" alt="Delete Site">
However, the test fails on the commented lines. When I do the same manually, a prompt appears as expected. Is it possible that popup windows are disabled when running WebDriver? I create my driver using new FirefoxDriver(), no extra params present. Do I need extra configuration? Or am I missing something else here?

Related

Button not working as expected in headless mode

I am using the latest chrome driver with selenium to test a web application hosted on Microsoft Azure.
The script starts by logging into the web application. An authentication window opens that requires the user to login through Azure and then press a, "Grant" button that will allow the web application to speak to a Therefore database to populate a few metadata fields.
This all works perfectly when headless mode is disabled. However, when headless mode is enabled it seems as though the, "Grant" button doesn't function. I am logging and taking screenshots during this process, which is how I know that the "Grant" button element is found and is being clicked. The button becomes highlighted when clicked, which is shown in the screenshot, but nothing happens and the authentication window times out in headless which kills the test.
I have tried different clicking methods, but this made no difference as the element is found and is being clicked. I have also pressed the, "Sign in as a different user" button on the form to ensure that the .click() method is functioning as expected, which of course works. I tried to add longer wait times but to no avail.
I have also added the following Chromium driver options:
ChromeOptions options = new ChromeOptions();
options.addArguments("enable-automation");
options.addArguments("--headless");
options.addArguments("--start-maximized");
options.addArguments("--window-size=1920,1080");
options.addArguments("--no-sandbox");
options.addArguments("--disable-extensions");
options.addArguments("--dns-prefetch-disable");
options.addArguments("--disable-gpu");
options.addArguments("--incognito");
options.addArguments("--disable-web-security");
options.addArguments("--allow-running-insecure-content");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--allow-insecure-localhost");
options.addArguments("--disable-popup-blocking");
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
How I am clicking the element:
System.out.println("Grant permission...");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Grant')]"))).click();
What baffles me is how this works seamlessly when headless is disabled but not when it's enabled. I'm wondering if this could be a Chrome driver issue? However I know that this is unlikely.
Any recommendations are appreciated, thanks.
Adding button HTML as requested:
<form method="POST">
<p>Hello, Test</p>
<ul>
<li class="text-left">Act with your access permissions</li>
<li class="text-left">Allow continuous access while you are not online.</li>
</ul>
<p>
<button type="submit" name="submit.Grant" value="Grant" class="btn btn-primary btn-block">Grant</button>
<button type="submit" name="submit.Login" value="Sign in as different user" class="btn btn-outline-primary btn-block">Sign in as different user</button>
</p>
Try adding a wait. I mean use WebDriverWait to wait for the element to be clickable.
Something like:
WebDriverWait wait = new WebDriverWait(webDriver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div/div/div/div/div/div/div[2]/form/p[3]/button[1]"))).click();
Also you need to improve your locator.
Absolute XPaths are extremely fragile.
First, try to avoid very long CSS or Xpath expression. You can find the button you need to click on like this:
driver.findElement(By.xpath("//button[contains(text(),'Grant')]"));
This code is more readable. If the site will be changed and the element will be moved to another div or span - your code will not work if it relays on the structure of all the divs and spans.
Next, never just click on an element of hover over it in a Selenium test. First check if the element is clickable, then click:
WebElement term = driver.findElement(By.xpath("//button[contains(text(),'Grant')]"));
WebDriverWait wait = new WebDriverWait(webDriver, 20);
wait.until(ExpectedConditions.elementToBeClickable(term));
term.click();

Sendkeys not working on search inputfield

I am trying to access an input field with a dynamically changing ID, and using other ways to find the element doesn't seem to work either.
This is the input field:
<input data-v-44dd203d="" type="search" placeholder="Search users" class="form-control form-control-md" inputmode="search" id="__BVID__27">
This is my current code in Java:
driver.get("website");
driver.findElement(By.xpath("//input[#type='search']")).sendKeys("name");
I am not getting any error, but when i am running the code, the website opens but nothing happens to the field. Seems i cannot access it, if it makes sense.
I had similar issue once and adding a click to the element first and then send keys worked for me.

Interrupting a file upload via Selenium using Java?

I'm trying to interrupt a file upload which doesn't seem possible. It seems that nothing gets executed beyond elem.sendKeys("filename.txt") until the entire file is uploaded. Further, none of the buttons are clickable although available when checked via Firebug or testing manually.
Automating the file upload and the interrupt by clicking a cancel file upload button (which is clickable when doing the test manually) leads to a test failure with the following exception: ElementNotVisibleException: Element is not currently visible and so may not be interacted with.
Is there any way I can enforce the interrupt? are there any other means that aids doing the same exact thing?. I'm using Firefox for this test.
HTML:
<div style="padding-left: 40px;">
<input id="upload" type="file" multiple="" label="File" name="upload[]" size="50">
</div>
Java:
WebElement elem = driver.findElement(By.id("upload"));
elem.sendKeys("filename.txt");
driver.navigate().refresh();
System.out.println("hi");
I have failed to see a point in doing that, but here we go:
I believe u can't stop the elem.sendKeys() method in Selenium(which does the actual upload).
Using it on a input text to set a string is the same as actually uploading a file. It's just Selenium trying to set some values on your input.
One way around would be clicking on a cancel upload button you have implemented.
Since you have tried that, this exception ElementNotVisibleException: Element is not currently visible and so may not be interacted with means exactly the that element exists (Selenium have found it on the DOM) but its not visible (the element has some properties like overflow: hidden display:none etc)
My best guess is that you should work that out and try clicking your button that implements an action of stopping the upload.

Click on element does not produce expected outcome using selenium webdriver

First the specs, I am writing Selenium Webdriver tests in java for an application written largely in ExtJS and running on the firefox browser v14. The interesting thing is that Selenium can find the element that I want to click, but the click does not seem to be executed or if it is getting executed the desired outcome (a popup appearing) does not happen. I have also verified in Selenium IDE that the element I am looking for (a span element that I locate via Xpath) exists, but in Selenium IDE I run into the same issue of not being able to click on it.
If I manually click on the button the popup window appears asking for what file I want to upload. I have also tried other elements such as the span's parent 'button' element and parent 'em' element and parent 'div' element all with no luck.
What kills me is that I have been writing Selenium Tests for this application for weeks now and always been able to use this method to click on buttons and for this particular button it no longer works.
WebElement uploadButton = driver.findElement(By.xpath("//span[contains(text(), 'Upload Popup')]"));
uploadButton.click();
Edit 1: Thought that the code of the button itself might help
<button id="filefield-1158-buttonEl-btnEl" class="x-btn-center" autocomplete="off" role="button" hidefocus="true" type="button" style="background-color: transparent;">
Note the id is the dynamic id created by ExtJS
It can be because of lots of reasons. I would suggest that you debug while your test is running.
It is possible to install FireBug in your Selenium Firefox instance:
File file = new File("firebug-1.8.1.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1"); // Avoid startup screen
WebDriver driver = new FirefoxDriver(firefoxProfile);
I assume you can launch your test via JUnit (in an IDE like Eclipse). Launch your test in debug mode and set a breakpoint just before clicking on the button. Inspect the html code via FireBug then. It might give you a start.
Another posibility is to select the button by css class (By.className) or selector (By.cssSelector):
WebElement uploadButton = driver.findElement(By.className("x-btn-center"));
uploadButton.click();
If there are multiple buttons on the page you would have to use
List<WebElement> buttons = driver.findElements(By.className("x-btn-center"));
WebElement uploadButton = buttons.get(index);
uploadButton.click();
Try this out:
driver.findElements(By.xpath("//button[#class='x-btn-center']").click()
or
driver.findElements(By.xpath("//*[#class='x-btn-center']").click()

Java Selenium Firefox Driver - Textbox Onchange Issue

Running Java/Selenium 2.3 (and 4) using Firefox Driver on centos
Trying to test against a site that has form with a text type, with an onchange. Tried to insert the text using the sendkeys, and then changing focus by doing a select/click on another term in the form. Tried to do a fireevent as well. (Doesn't appear to be supported in the 2.3 sel)
Searched the net as well with no luck.
Basically, trying to get a solution to how to do an insert into a textbox for selenium/firefox driver, so the inserted text actually appears in the textarea, which means the onchange event gets fired.
The test html is::
<td rowspan='4' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divCLASS_SRCH_WRK2_SUBJECT$69$'><input type='text' name='CLASS_SRCH_WRK2_SUBJECT$69$' id='CLASS_SRCH_WRK2_SUBJECT$69$' tabindex='31' value="" class='PSEDITBOX' style='width:60px; ' maxlength='8' onchange="addchg_win0(this);oChange_win0=this;" />
</DIV></td>
The test code is::
driver.findElement(By.name("CLASS_SRCH_WRK2_SUBJECT$69$"))
.sendKeys("ACG");
driver.findElement(By.name("CLASS_SRCH_WRK2_SUBJECT$69$"))
.sendKeys("");
Select sCourse= new Select(driver.findElement(By.id("CLASS_SRCH_WRK2_ACAD_CAREER")));
sCourse.selectByValue("");
The test sets the textelement, and then sets the select/option of a select item, which should trigger the change in focus. I also tried to clear, and reset the text, thinking that might trigger the onchange..
A solution to this would help a lot of people who've been looking for the same thing!!
Thanks
Have you tried doing a tab after sendKeys?
You can do a
driver.findElement(By.name("CLASS_SRCH_WRK2_SUBJECT$69$"))
.sendKeys("\t");
I had a similar problem (though my trigger was onBlur). I called the blur() method directly. In your case it would be
driver.findElement(By.name("CLASS_SRCH_WRK2_SUBJECT$69$")).sendKeys("ACG");
((JavascriptExecutor)driver).executeScript( "$('[name=\"CLASS_SRCH_WRK2_SUBJECT$69$\"]' ).blur() );
You might need a different function other than blur().

Categories

Resources