Sencha Disabled Button and Selenium Webdriver - java

I have a button on a website built with sencha/extjs. Currently the button id is savebutton-1550-btnEl, but this changes everytime the page is loaded. I know that the button is disabled, but for testing purposes, I'd like to set this button as enabled, and then click it.
How would I go about finding this element each time, and then disabling it and clicking it with Java Selenium?
I'm guessing I'll have to execute some javascript, but I'm having a hard time finding the target for the javascript.

To locate the element you will have to use part of the DOM surrounding the element as a unique locator. It is impossible to give a more specific answer without seeing the DOM you are working on, but you may try something like:
WebElement saveButton = driver.findElement(By.xpath("//button[text()='Save']");
For changing the element to enabled, take a look at this answer: Selenium Webdriver - click on hidden elements

Also a longer term solution might be to work with development to see if they can build in a unique locator especially since this probably wont be the only object that you have problems with. At my company we use the "class" field to uniquely identify objects in extjs.

I override the the class "AbstractComponent":
Ext.define('Foo.overrides.AbstractComponent', {
override: 'Ext.AbstractComponent',
onBoxReady: function () {
var me = this;
var el = me.getEl();
if (el && el.dom && me.itemId) {
el.dom.setAttribute('data-test', me.itemId);
}
me.callOverridden(arguments);
}
});
If you set in configuation of the button the "itemId", you
can be accessed the button via seleniumas follows:
IWebElement element = webDriver.FindElement(By.XPath($"//*[#data-test='{itemId}']"));

Related

Selenium Java Drag and Drop with HTML5

In several threads here, there is a work-around posted for selenium drag and drop with pages that use HTML5 for drag and drop. This work-around involves using javascript to simulate the drag and drop, for example Unable to perform HTML5 drag and drop using javascript for Selenium WebDriver test, and https://gist.github.com/rcorreia/2362544. This solution works well on this page, http://the-internet.herokuapp.com/drag_and_drop.
The general approach is to read the javascript file here (https://gist.github.com/rcorreia/2362544#file-drag_and_drop_helper-js) into a string, referred to as 'jsfile' below.
then in selenium (with java), pass in the css selectors for the source and the destination, where #column-a is the id of the source and #column-b is the target.
((JavascriptExecutor) driver).executeScript(jsfile +"$('#column-a').simulateDragDrop({ dropTarget: '#column-b'});");
It works like a champ on that page.
However, a similar approach does not seem to work on this page, https://crossbrowsertesting.github.io/drag-and-drop.html. Nothing happens when I run
((JavascriptExecutor) driver).executeScript(jsfile +"$('#draggable').simulateDragDrop({ dropTarget: '#droppable'});");
I have pages that seem to behave like this second page (eg no drag and drop). As a first step in understanding this, I'd like to get an idea why this approach does not seem to work in the latter case here.
On re-testing https://crossbrowsertesting.github.io/drag-and-drop.html, it looks like the straight-forward use of the Actions class does the trick for drag and drop. In the particular app that I am testing, which is set up with some additional code to help with accessibility, I was able to get drag and drop happening by setting focus on the first element and hitting the return key, then setting the focus on the target element and hitting return again. I am fairly sure that this is custom event handling, so may not work in other applications. Just in case, I've posted code here which does this in selenium.
public void dndHtml5(String xPathSource, String xPathDestination) {
clickEnterKeyOnElement(xPathSource);
clickEnterKeyOnElement(xPathDestination);
}
public void clickEnterKeyOnElement(String xPath) {
setFocusOnElement(xPath);
WebElement target=element(xPath);
target.sendKeys(Keys.ENTER);
}
public void setFocusOnElement(String xPath) {
WebElement element = element(xPath);
Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();
}
public WebElement element(String xPath){
return driver.findElementByXPath(xPath);
}

Java HTMLUnit Testing Navigation Buttons

I'm quite new to Selenium HTMLUnit so I'm looking for some help in creating a basic test for a navigation button in my Spring Boot app.
I'm trying to put together some basic tests to make sure that the navigation buttons in my webapp work as expected. My approach to this is to find the required navigation button by Id, click it, then check that the current URL is the new page I was expecting to be on.
...
private HtmlUnitDriver driver = new HtmlUnitDriver(true);
...
#Test
public void navigationTest() {
driver.get(BASE_URL);
WebElement button = driver.findElement(By.id("navigation_button_id"));
button.click();
String currentUrl = driver.getCurrentUrl();
Assert.assertThat(currentUrl, is(BASE_URL + "some_other_page"));
driver.close();
}
I've attempted to use the .click() method (shown above), I've also attempted to use an Actions object instead of the simpler .click() method (see below) to perform the navigation but this still did not work.
Actions actions = new Actions(driver);
actions.click(button).build().perform();
The behaviour at the moment is that the URL is not changing from the BASE_URL (e.g. http://localhost:8080), I have verified that it works manually (clicking around in person) but I can't get the test to click the button and tell me that the URL has changed (showing that the user has been taken to the new page e.g. http://localhost:8080/some_other_page).
Can anyone offer some advice to get this working? I just need the simplest means of testing basic navigation is working in HTMLUnit.
I would suggest you to use below code for Navigation purpose :
To go forward :
driver.naviagte().forward();
To go backward :
driver.navigate().back();
For solving your issue , when you are going forward/backward : Wait for some element by using this code :
WebDriver wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated("some element locator"));
Then get the currentUrl by using driver.getCurrentUrl(); code.
Hope this will help you to resolve your issue.
Please let me know if you have any concerns related to this.
The way I remember doing it is to wait/ assert for the new page to load. If you can't do that, add a wait of few seconds before getCurrentUrl()

Selenium element can not be found within iframe

I am trying to retrieve a JSON element, the problem is that in the source code it doesn't exist, but I can find it via inspect element.
I have tried with
C.driver.findElement(By.id("ticket-parsed"))
and via XPath
C.driver.findElement(By.xpath("//*[#id=\"ticket_parsed\"]"));
and I can't find it.
Also
C.driver.switchTo().frame("html5-frame");
System.out.println(C.driver.findElement(By.id("ticket_parsed")));
C.driver.switchTo().defaultContent();
i get
[[ChromeDriver: chrome on XP (1f75e50635f9dd5b9535a149a027a447)] -> id: ticket_parsed]
on
driver.switchTo().frame(0) or driver.switchTo().frame(1)
i get that the frame doesn't exists
and at last i tried
WebElement frame = C.driver.findElement(By.id("html5-frame"));
C.driver.switchTo().frame(frame.getAttribute("ticket_parsed"));
an i got a null pointer exception
Here's an image of the source:
what am I doing wrong?
Well!
The element #ticket-parsed is in iFrame. So, you can click it without getting into an iframe.
Here is the code to switch to iFrame,
driver.switchTo().frame("frame_name");
or
driver.switchTo().frame(frame_index);
In your case,
driver.switchTo().frame("html5-frame");
After switching into the iframe, you can click that element using either XPath or CSS.
C.driver.findElement(By.id("ticket-parsed"))
NOTE:
After completing the operation inside the iframe, you have to again return back to the main window using the following command.
driver.switchTo().defaultContent();
I didn't found a solution with my excising setup,but i did found a js command which gets the object correctly
document.getElementById("html5-frame").contentDocument.getElementById("ticket_parsed")
you can integrate js commands like this
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript(*yourCommandHere*);
if you want to get the output of the command just add the word return before your command (in this specific situation it didn't work but in any other situation it did)
*TypeOfData* foo = js.executeScript(return *yourCommandHere*);
at last because of limited time i had to use unorthodox methods like taking screenshots and comparing the images if they are exactly the same
Thanks for the help

Selenium Webdriver : Dismiss Chrome Alert

I want to select the check box Prevent this page from creating additional dialogs before selecting ok to close the alert
Currently i am using
Alert alert=driver.switchTo().alert();
//check the checkbox
alert.accept();
As an normal interactive user to check the check box i have to use a combination of <Tab> + <Space/Enter> keys. The <Tab> shifts the focus to the check-box and the <Space/Enter> checks the check-box.
Solutions Tried
I tried using Java sendKeys mechanisms ( Robot class, driver.sendKeys(), etc.), but an UnhandledAlertException is getting thrown.
I tried using alert.sendKeys which is different from driver.sendKeys() but it too failed
//check the checkbox
alert.sendKeys("\t");
alert.sendKeys("{TAB}");
alert.sendKeys("\uE004");
alert.sendKeys("\\U+0009");
alert.sendKeys(Integer.toString(KeyEvent.VK_TAB));
I am trying to avoid robot class as much as possible as i need to run the test in grid in which case robot class will not work.Any pointers on how to send keys to the alert window ?
Temporarily i could do it using a javascript window.alert = function() {}; by simply overriding the alert with an empty function just curious to know if it could be done with webdriver functions like alert.sendkeys or any other methods ??
Any help is greatly appreciated!!

Set focus on WebElement?

i'm currently testing the GUI of my application, and i wanted to know if it's possible to set the focus to a WebElement ?
I'm using Selenium 2.0 and the webdriver.
So i'm looking for something like that : driver.findElement(xxxx).setfocus();
Thank you.
EDIT :
I've alreardy tested that kind of tricky things
// getting the element WebElement
eSupplierSuggest = driver.findElement(By.xpath("..."));
//get le location and click
Point location = eSupplierSuggest.getLocation();
new Actions(driver).moveToElement(eSupplierSuggest, location.x, location.y).click();
//or
//directly perform
new Actions(driver).moveToElement(eSupplierSuggest).click().perform();
i red somewhere that the click focus the element, but in my case, nothing works. lol
PS : this is a sub-question of that original post Click on a suggestbox, webdriver
I normally send an empty key to the element so it gets focused. So for example:
element.send_keys ""
In order to set focus on the element you can use executeScript method as described below :
JavascriptExecutor js;
js.executeScript ("document.getElementById('x').focus()");
Once the focus is set you can easily use send_keys provided by webdriver api.
Try using cssSelector for the autosuggestion click as shown below and let me know if you are still facing the issue.
// supplier ops, i find and type data into the input
WebElement eSupplier = driver.findElement(By.id("supplier:supplierOps_input"));
eSupplier.sendKeys("OPS1");
sleep(5); // wait the suggestbox
// i find the suggestbox
WebElement eSupplierSuggest = driver.findElement(By.cssSelector("css path of that specific value in the auto suggestion box"));
eSupplierSuggest.click();
sleep(5); // wait the refresh for the next field supplierAddress
There is no function in the WebDriver API to set focus on an element.
If you want to do it you would have to write some JavaScript to set focus and then use a JavaScriptExecutor to run the JavaScript.
Make sure, that you are not changing the frame....
Other wise .click() should do the trick

Categories