Using selenium and java, I need to highlight the below text using a mouse click and move, After highlighting, the text I can see another popup and I need to do some validation on that popup.
<h2 class="lstw">16.1 This is sample text, another, and heading</h2>
This is my current code and it is not working sometimes. Please note KeyDown or set CSS through javascript is cannot use here.
public boolean userMoveHighlightText(String text){
String secName = "//h2[text()='"+text+"']";
getFrame(); // switch to iframe
By element = getLocator(secName, BY_TYPE.BY_XPATH);
Actions action = new Actions(getWebDriver());
WebElement content = $(element);
action.moveToElement(content, 0, 0).clickAndHold().moveToElement(content, content.getSize().getWidth() / 2, content.getSize().getHeight() / 2);
action.release().build().perform();
return true;
}
Replace the below code with the recommended:
action.moveToElement(content, 0, 0).clickAndHold().moveToElement(content, content.getSize().getWidth() / 2, content.getSize().getHeight() / 2);
Recommended:
action.moveToElement(content, 0, 0).build().perform()
//implicit wait
action.clickAndHold().moveToElement(content, content.getSize().getWidth() / 2, content.getSize().getHeight() / 2).release().build().perform();
Related
I'm using canvas (phaser.io game framework) to make games and would like to do selenium tests. Sadly I can't replay recorded actions on a canvas.
For example I can't replay the click on the button here https://phaser.io/examples/v2/input/button-open-popup
I get this in the log:
1.Trying to execute open on /examples/v2/input/button-open-popup... Success
2.Trying to execute selectFrame on index=0... Success
3.Trying to execute clickAt on css=canvas with value 422,502... Success
But nothing happens on the screen and the popup is not poping up.
Is there a problem with clicking on canvas through Selenium IDE or maybe I'm doing something wrong?
I did some automated tests for Phaser games.
Let's take an example, I have to click on a menu button.
The way I managed to click on the button precisely every time is that I created a html page, with the same width and height as my canvas ( first, I decided the size of the chrome window, for me I used 800x900, and then get the canvas size), and in my html page I only put javascript to output me the positions where I click.
So basically I created a html, with the same dimension as my canvas, and clicked on it at the approximate position of my canvas button.
Here is the code I've used for my tests:
var mainState ={
preload: function(){
},
create: function(){
game.stage.backgroundColor = '#71c5cf';
game.scale.pageAlignHorizontally = true;
game.scale.pageAlignVertically = true;
},
update: function(){
getcoordinates();
}
};
function getcoordinates(){
if (game.input.mousePointer.isDown){
var x = game.input.activePointer.position.x;
var y = game.input.activePointer.position.y;
console.log("x" + x, "y" + y);
var worldx = game.world.centerX;
var worldy = game.world.centerY;
console.log("world x" + worldx, "world y"+ worldy);
}
};
var game = new Phaser.Game(384,683, Phaser.CANVAS);
game.state.add('mainState', mainState);
game.state.start('mainState');
As for checking if my action was succesfull, I used JavascriptExecutor. And in Selenium I've created some functions that do just that, navigate to coordinates and execute click.
Problem : Mouse hover action is not highlighting the element . We are using selenium's mousehover() method.
What we tried : We tried the three solutions given in http://toolsqa.com/selenium-webdriver/mouse-hover-action/ link but none of it worked to achieve the goal.
Please let us know your advise or suggestion for achieving this.
which browser you are using because if you are using firefox and selenium 3.5 there is an issue regarding mouse actions which they are looking into it. I suggest try running it in chrome and check
Try JavaScript for mouse hover. Please give below code try and let me know if its works:
string javaScript = "var evObj = document.createEvent('MouseEvents');" +
"evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
"arguments[0].dispatchEvent(evObj);";
JavascriptExecutor js = = (JavascriptExecutor)driver;
//Pass element on which mouse hover to be performed
js.executeScript(javaScript,element)
Pass you webelement in above line on which you want to perform mouse hover.
I have achieved the long press event on empty text field.
Than Paste option is coming but ui-automator is not showing any id for that and no specific key code for the paste option.So how to achieve this click event on "empty text field" for paste option using c#-selenium appium method for android.
I have tried following method.But it is giving 'Not Yet implemented'.
//namespcaces
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.Interfaces;
using OpenQA.Selenium.Appium.iOS;
using OpenQA.Selenium.Appium.MultiTouch;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
public void pasteOperation(string elem){
AppiumWebElement element = FindElement(elem);
x = element.Location.X;
y = element.Location.Y;
new OpenQA.Selenium.Appium.MultiTouch.TouchAction(_driver).Press(element,x, y).Wait(2000).Release().Perform();
Dictionary keyEvent = new Dictionary();
keyEvent.Add("keycode", 50);
keyEvent.Add("metastate", 113);
_driver.ExecuteScript("mobile : keyevent", keyEvent);
}
I have also tried following link.
https://discuss.appium.io/t/how-can-i-click-paste-popup-to-paste-clipboard-data-in-android/3538
I have found the way to click on "paste" option using touch action.
/*
* Perform Paste text to empty text input field
*
* */
public void PerformPasteTextToEmptyElement(string elem)
{
AppiumWebElement element = FindElement(elem);
//Long press at center of empty text field for paste option
PerformLongPressOnCenterofElement(element);
//touch action on paste option above input field
TouchAction action = new TouchAction(_driver);
action.Press(element.Location.X + 30, element.Location.Y - 30).Wait(1000).Release().Perform();
}
/*
* Perform Long Press action on Center of the element
*
* */
public void PerformLongPressOnCenterofElement(AppiumWebElement elem)
{
TouchAction action = new TouchAction(_driver);
action.Press(elem).Wait(2000).Release().Perform();
}
I've written a Chrome/Opera extension and am considering porting it to Safari. It needs 2 specific capabilities and the absence of either would veto the whole plan.
Ability to read HTTPS urls - Chrome supports this as part of the "tabs" permission. Firefox (last I checked) does not.
Ability to dynamically change the appearance of the activation button. - In Chrome, this is achieved by having a single canvas on the background page (i.e. the button)...
-body- -canvas id="button_canvas" width="19" height="19"- -/canvas- -/body-
... and then dynamically changing it whenever necessary ...
var canvas = document.getElementById("button_canvas");
var context = canvas.getContext("2d");
var imageData = context.getImageData(0, 0, 19, 19);
// write stuff to the canvas
context.putImageData(imageData, 0, 0);
imageData = context.getImageData(0, 0, 19, 19);
// key element below
chrome.browserAction.setIcon({
imageData: imageData
});
If anyone can answer these two questions definitively before I buy a used Mac on Craigslist, I'd appreciate it. Thanks!
p.s. FWIW, my own Googling suggests that #2 is not possible in Safari. No idea about #1.
I believe both are possible.
For HTTPS URLs: In the Safari extension builder, under Extension Website Access, set Access Level to All and tick the Include Secure Pages checkbox.
To dynamically change the icon displayed on a toolbar icon, first set any icon in the Safari extension builder. Then if you want to change in response to a toolbar button click:
safari.application.addEventListener('command', performCommand, false);
function performCommand(event) {
if (event.command === 'changeIcon') {
event.target.image = safari.extension.baseURI+'othericon.png';
}
}
Under other circumstances, you can iterate your toolbar buttons to modify the one you want:
var toolbarButtons = safari.extension.toolbarItems;
for (var i = 0; i < itemArray.length; ++i) {
var item = toolbarButtons[i];
if (item.identifier === "mybutton") {
item.image = safari.extension.baseURI+'othericon.png';
}
}
I have using recursiveTreeNodesAdaptor in seam. And I want to add child this tree via contextMenu and when user click right node of tree open context menu and click left then open modal panel. I want to do this.
When I try to write selenium test this flow I dont click to rigt and open contextMenu. I try to selenium.contextmenu(xpath) but i'm fail.
So, How can I click to right
I tried to simulate right click with user extension. My function is below:
Selenium.prototype.doContextMenuClick = function(element){
var evt = document.createEvent('MouseEvents');
var RIGHT_CLICK_BUTTON_CODE = 2; // the same for FF and IE
evt.initMouseEvent('contextmenu', true, true,
document.defaultView, 1, 0, 0, 0, 0, false,
false, false, false, RIGHT_CLICK_BUTTON_CODE, null);
if (document.createEventObject){
// dispatch for IE
return document.fireEvent('onclick', evt);
}
else{
// dispatch for firefox + others
return !document.dispatchEvent(evt);
}};
I managed to call the function from IDE. But, now, I get the error "this.waitForCondition is not a function". What's wrong? Do you have any idea?
Thanks in advance.
Try using fireEvent on that element.
You should also take a look at this post which seems to be about the same issue.
The latest selenium has a namespace called OpenQA.Selenium.Interactions Namespace. check it out here.
see my other post here