I'm working on a test script that works with Appium (Selenium) on Java for mobile devices.
In the script, there's following codes:
_driver2.swipe(startXL, startYL, endXL, endYL, 100);
and
_driver2.tap(1, startX, startY, 100);
These methods swipe a game object or click on a button in a game.
Here are some details:
Swiping method is using absolute locations on screen. Tap method is using a location that is calculated by an image recognition function.
Here's my question:
While the test is running; if device's screen is different than expected, like push notifications or native dialogs, test doesn't stop and still tries to find an image/location and then clicks/swipes.
I want to make it stop if there's a different screen than expected or the game object didn't move.
My question is probably not clear but I can give you details comment by comment.
Thank you in advance.
You can use an simple if else condition
If element is found { Swipe }
Else { Perform other action }
Step 1:
public boolean isElementFound(String element)
{
try{
WebElement webElement = driver.findElement(By.xpath(element));
System.out.println("isElementFound : true :"+element);
}catch(NoSuchElementException e){
System.out.println("isElementFound : false :"+element);
return false;
}
Step 2 : while(isElementFound(String element))
{
TouchAction touchAction = new TouchAction(appiumDriver);
System.out.println(startx+" "+starty);
System.out.println("Entering swipe");
System.out.println("Swipe from "+startx +" " +starty +"to" +endx +" " +endy );
touchAction.press(startx, starty).waitAction(duration).moveTo(endx,endy).release().perform();
// WebElement webElement = appiumDriver.findElement(By.xpath(element));
webElement.click();
}
Related
I am writing some script automation in java which searches some recordings for mobile number and finds multiple recording in gridview. Next to this I am trying to download all recordings by clicking download (multiple)button(s).
I have written below code but does not work
public static Screen s = new Screen();
Iterator <Match> matches =s.findAll("downloadbtn_wh.png"); // s is screen
Pattern pButton = new Pattern("downloadbtn_wh.png");
Match mButton;
while (matches.hasNext()) {
Match m = matches.next(); // m now could be inspected with debugging
s.click(m); // click on drop-down
if ((mButton = s.exists(pButton))!=null) {
// checks for button image and saves the match
s.click(mButton); // just click the match, do not search again
break;
}
}
This script stops after clicking on first download button but I expect that it should click on every download button in gridview.
Images
The below code will perform what you require.
#Test
public void multiplePattern() throws FindFailed{
ImagePath.setBundlePath("C:\\patterns\\");
Screen s = new Screen();
Iterator<Match> it = s.findAll("downloadArrow.png");
while(it.hasNext()){
it.next().highlight(1);
}
}
NOTE: Please note that in the above example I am not actually clicking but only highlighting the detected patterns just to visualize the process. Just replace highlight(1) with click() before you use it in your script.
Screen scn = new Screen();
Iterator <Match> itr = scn.findAll("image");
{
while(itr.hasNext())
{
itr.next().click();
}
}
I can switch between two tabs/windows but my requirement is to know or get active window between them.
In my project, on a click of a webElement of a page a random pop(tab/window) gets opened and I would like to know whether that(new) window has focus or my original page.
I tried to use JNA Api to get the active window and its title but my web page is
remotely located.
Perfect solution is greatly appreciated.
Thanks
driver.getTitle() will give you the title of the page that you can use to determine which page you are on or if you are on the page where you want to be and then use the logic to switch window if required. getTitle() returns a String and you can use one of the string methods to compare the title, for example:
String title = getDriver().getTitle();
if(!title.equals("Expected Title")) {
//may be you would like to switch window here
}
String title = driver.getTitle()
This will give you the title of the page which you can refer to using Selenium to figure out which page the driver is currently on.
I wrote my own method to switch to a window if the window title is known, maybe some of this would be helpful. I used Selenide (Java) methods for this, but if you've got Vanilla WebDriver, you can achieve the same thing
/** Switches user to window of user's choice */
public static void switchToWindow(String windowTitle) {
WebDriver driver = getWebDriver();
// Get list of all open tabs - note behaviour may be different between FireFox and Chrome.
ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
// iterate through open tabs. If the title of the page is contained in the tab, switch to it.
for (String windowHandle : driver.getWindowHandles()) {
String title = getWebDriver().getTitle();
driver.switchTo().window(windowHandle);
if (title.equalsIgnoreCase(windowTitle)) {
break;
}
}
}
This method might not be lightening fast, but it will iterate through current open windows and check the title matches the one you've specified.
If you want to assert the title, you could use the xpath selector:
String pageTitle = driver.findElement(By.xpath("//title[text() = 'Title you looking for']"));
This is a dumb example with you can surround with try/catch, implement assertions or other technique to have the result you need.
In JavaScript, for me this worked. After clicking on the first link of bing search results in edge, my link opened in a new tab. I explicitly mentioned to stay in the same tab.
async function switchTab() {
await driver.getAllWindowHandles().then(async function (handles) {
await driver.switchTo().window(handles[1]);
});
}
//get initial window handles
Set<String> prevWindowHandles = driver.getWindowHandles();
while(true){
//get current window handles
Set<String> currWindowHandles = driver.getWindowHandles();
//if one of the current window handles not equals to
//any of the previous window handles,switch to this window
//and prevWindowHandles = currWindowHandles
for(String prevHandle : prevWindowHandles){
int noEqualNum = 0;
for(String currHandle : currWindowHandles){
if(!currHandle.equals(prevHandle))
noEqualNum++
}
if(noEqualNum == currWindowHandles.size()){
driver.switchTo().window(currWindow);
prevWindowHandles = currWindowHandles;
break;
}
}
}
I'm trying to use Selenium to test out a form on a website that consists of multiple pages. There are modals that are suppose to appear on some of the pages based on options that are selected. Currently I'm having trouble getting Selenium to work properly with Fireforx and the modals. Chrome acts as expected, and I haven't bothered with I.E. yet.
When I work through the pages manually with Firefox, everything works as expected.
When I run my Selenium script the modal displays like a Windows pop-up window, not a modal.
I'm using driver.switchTo().alert().accept(); to handle the modals, and the first modal I encounter will close, but once I get to the next page the Selenium code is unable to find any of the elements on the page.
Here is the code I use to click a button:
public void pushButton(String[] values) {
System.out.println("\t Click (" + values[1] + ")");
setLocator(values[0], values[1]);
try {
clickWhenReady(locator).click();
} catch (Exception e) {
System.out.println("Could not find id or xpath value: " + values[1]);
e.printStackTrace();
}
}
private void setLocator(String byType, String value) {
if(byType.toUpperCase().equals("ID")) {
locator= By.id(value);
} else if(byType.toUpperCase().equals("XPATH")){
locator= By.xpath(value);
}
}
private WebElement whenReady(By locator){
WebElement element = (new WebDriverWait(driver, 30))
.until(ExpectedConditions.presenceOfElementLocated(locator));
return element;
}
private WebElement clickWhenReady(By locator){
WebElement element = (new WebDriverWait(driver, 30))
.until(ExpectedConditions.elementToBeClickable(locator));
return element;
}
First of all, modals are not alerts so, driver.switchTo().alert().accept(); will probably not going to buy you anything.
The real issue could be the modals take longer to fade out and blocking Selenium to interact with the page. Your best bet will be to try waiting for the modal to completely disappear from the dom and then try interacting with the elements. For that you can use invisibilityOfElementLocated of ExpectedConditions or similar mechanism. See this
I'm using the Highchart librairy with GWT to display multiseries Donut charts.
I would like to be able to click on one point (one series' subpart) and retrieve within the event handler the name of this point.
I tried "setPointLegendItemClickEventHandler" in the "PiePlotOption" that I set to my series but that's not working.
I can't find anything in the documentation that helps me.
Any idea ?
You just need to add click event listeners in your highcharts. For capturing the click event on the inner level series, add the following code in the Series (browsers) tag inside highcharts:
point: {
events: {
click: function(event) {
alert("Name: "+this.name + " X: " + this.y + " Y: " + this.x);
}
}
}
and for capturing the click on on the outer level 2 series, add the following inside plotOptions>Pie tag:
point: {
events: {
click: function(event) {
alert(this.name);
}
}
}
You will get the name as well as X, Y values of each point clicked.
See the DEMO here.
So I found the solution. See below :
myDonutChart.setSeriesPlotOptions(new SeriesPlotOptions().setPointClickEventHandler(new PointClickEventHandler() {
#Override
public boolean onClick(PointClickEvent pointClickEvent) {
// log pointClickEvent.getPointName();
return false;
}
}));
There you go.
String parentHandle = driver.getWindowHandle();
driver.findElement(By.id("ImageButton5")).click();
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
driver.findElement(By.id("txtEnterDescription")).sendKeys("Test");
driver.findElement(By.id("chklstAllprocedure_0")).click();
I used this code and I got the error as
"Exception in thread "main"
org.openqa.selenium.NoSuchElementException: Unable to find element
with id == txtEnterDescription (WARNING: The server did not provide
any stacktrace information) Command duration or timeout: 30.05
seconds". The HTML code for this text box is ""
Please help me out of this
you may face "NoSuchElementException" only in two case for sure.
1.The Element is yet to be Loaded
- Have an appropriate wait logic here.
2. You may try to locate the element wrongly .
- Double Check your Xpath/ID(what ever)
- Make sure you are in the same frame where the element is present.If not, switch to the frame then.
Just make sure you are switching to the right window
Reason 1 : Just make sure you are switching to the right window
I have an utility method to switch to the required window as shown below
public class Utility
{
public static WebDriver getHandleToWindow(String title){
//parentWindowHandle = WebDriverInitialize.getDriver().getWindowHandle(); // save the current window handle.
WebDriver popup = null;
Set<String> windowIterator = WebDriverInitialize.getDriver().getWindowHandles();
System.err.println("No of windows : " + windowIterator.size());
for (String s : windowIterator) {
String windowHandle = s;
popup = WebDriverInitialize.getDriver().switchTo().window(windowHandle);
System.out.println("Window Title : " + popup.getTitle());
System.out.println("Window Url : " + popup.getCurrentUrl());
if (popup.getTitle().equals(title) ){
System.out.println("Selected Window Title : " + popup.getTitle());
return popup;
}
}
System.out.println("Window Title :" + popup.getTitle());
System.out.println();
return popup;
}
}
It will take you to desired window once title of the window is passed as parameter. In your case you can do.
Webdriver childDriver = Utility.getHandleToWindow("titleOfChildWindow");
and then again switch to parent window using the same method
Webdriver parentDriver = Utility.getHandleToWindow("titleOfParentWindow");
This method works effectively when dealing with multiple windows
Resaon 2 : wait for the element
WebdriverWait wait = new WebdriverWait(driver,7000);
wait.until(ExpectedConditions.visbilityOfElementLocatedBy(By.name("nameofElement")));
Reason 3 : check if the element is in a frame if yes switch to the frame before
driver.switchTo.frame("frameName");
Let know if it works ..
Guess the problem is in your switch to logic. I think when you are switching it is passing the code of the parent instead of the child. For this scenario create two local variables parent and child. Loop through the window handles using an iterator and set the parent and child window id's to the variables and pass the child id to the switchTo method. This should work. Keep me posted. Happy coding.