Safari fail to click only on the first test of the group TestNG - selenium

When I run several tests using TestNG on Safari, the first test always fails because of the click operation.
It doesn't matter what test it is as long as it has a click in it, even if I run the same exact test couple of times (using #Test(groups = {"test"}, invocationCount = 4)) only the first one can't perform a click and the others pass with no issues.
When I say click is not performed I mean the outcome of the click is not happening (for example opening a new tab or clicking on a button to change something), there is no error so the code executes the click function and move on although the click did nothing.
What could cause that difference of behavior between the first test and the tests that come after?
Here is the code of the click function:
public void clickOnWebCoords(WebDriver driver, WebElement element, Point pointToClick) {
Actions actions = new Actions(driver);
actions.moveToElement(element, pointToClick.x, pointToClick.y).perform();
actions.click().perform();
}
Safari version: 14.0.2, Selenium version: 3.141.59

Related

Is the Selenium click() recognized as human or automation?

My scenario which produces the question goes something like below:
I enter a webpage via normal means, next I press on a button, to start a HTML5 application on this webpage, this application is inside an iFrame. On application start I'm being prompted to either turn the sound on or off. At this point there are two possible outcomes:
1. When I answer this prompt manually, new buttons appear in the application window, as expected.
2. When I answer this prompt through automation via Appium, new buttons do not appear.
Now to the question:
To answer the prompt I use the click() method from Selenium. Is it possible that this click() is not considered to be executed by a human and therefore doesn't trigger necessary things? And since I don't have access to the source of the application can I force the Selenium click() to look exactly like a human click?
Here is the code I use to execute the mentioned click:
//Application loading up, hence the sleep
Thread.sleep(5000);
AppiumTestBase.getDriver().switchTo().frame("e_iframe");
Thread.sleep(5000);
WebElement soundOff = AppiumTestBase.getDriver().findElement(By.id("soundOff"));
AppiumTestBase.getStandardWaitTime().until(elementToBeClickable(soundOff));
soundOff.click();
The program is able to find and switch in to the iFrame, there are no cross-origin issues either. The AppiumTestBase is just there for initializing the driver, setup capabilities etc. I also tried clicking the element via Actions and JavaScript, but there was no change in behavior.
In C# a workaround I've found to actually take control of the mouse and move it/click with it is to use "Microsoft.VisualStudio.TestTools.UITesting" for the Keyboard/Mouse libraries. From there, you can tell it "Mouse.Click(new Point(X, Y));"and it will move your mouse to that location and click.
Sample Code:
using Microsoft.VisualStudio.TestTools.UITesting;
var soundOff = AppiumTestBase.getDriver().findElement(By.id("soundOff"));
Mouse.Click(new Point(soundOff.Bounds.X, soundOff.Bounds.Y));

assertConfirmation not displaying in selenium IDE

I am new to Selenium, I am using Selenium IDE to record test scripts.
I am having trouble while executing one of the recorded test cases in Firefox. It is a simple test case which clicks a button and then clicks on OK button of the pop up window .This is the page link for which I am creating script.
Below are the three steps recorded from IDE
The problem is that after the second step, i.e., click step, no pop up is being shown, the same happens in the next step.
The pop up is not displayed yet the test case gets executed correctly.
Could someone please let me know why I am not able to view the pop up ?

Selenium webdriver Assert click element worked

I am automating a web page, and I am having lots of issues in clicking in a element, so I implemented this
try {
element.click();
} catch (WebDriverException e) {
clickJS(element);
}
clickJS is a method I wrote to click using javascript approach, it usually works, however I am having issues when the expression to click does not throw any exception, but in the future steps will fail because it did nothing. Is there a way for assert a click has 'worked' even though it did not throw any exception.
ps: Iam sure the webelement is clickable
PS: I am using chrome webdriver
There's no generic way to determine if a click was successful because a click could do just about anything... navigate to another page, click a checkbox, dynamically load another part of the page, etc.
In general, I would say that this is not the right approach if you are trying to automate a customer scenario. For example, you attempt a click but didn't anticipate some dialog popping up. Your normal click would throw an exception that another element would receive the click but your JS click would succeed. You shouldn't want that click to succeed because a user couldn't click that element without dealing with the dialog first. This may cause a strange failure down the road that will be hard to trace. Do a "normal" click each time. As you run the script, you will find intermittent failures. Investigate them and find solutions, e.g. wait for some dialog to close because 1 in 10 times it closes slowly and so on. In the end, you will have a more robust suite.
You can check whether your click is actually happened or not in some of the scenarios.
Ex: most of the case click will lead to some change in the UI that might be UI change,HTML source code change or some text might change.if nothing is changing after your click then the click itself is no use.
Solution: take a snap shot before ur click and take a snapshot after click then compare both images if u found any difference then ur click performed successfully else not.

Selenium stops running after click() function runs

So I am writing a class in eclipse using selenium and autoitx4java libraries, now I have been having an issue that I believe is selenium specific (meaning having nothing to do with autoIt).
Basically when I arrive at the splash page for the website I am attempting to test, the first thing I do is find the element for the login page via XPath, and click on it using the click() method. This is supposed to open up a certificate window.
Once the click() method runs a certificate window comes up, however, for some reason the selenium test does not keep running lines of code. Instead it pauses at the line the click function was executed. The code looks a bit like this:
WebElement login = driver.findElement(By.xpath("example_xpath"));
login.click();
// login.sendKeys(Keys.ENTER);
System.out.println("login clicked");
Class.selectCert();
(Where "Class" is just a class created for this specific test.)
So when the code is run in Selenium, the line "login clicked" is never printed out. As you can see, the alternate sendKeys(Keys.ENTER) function has been tried. When I used this function, literally nothing happened, and the code continued as if that line of code did not exist.
Note: When I highlight the element and manually press ENTER, the certificate window actually pops up.
This is where things get weird. When I remove the line for the click function, and replace it with a pause() function which gives me ample time to click on the element manually. The code runs perfectly fine, the "login clicked" is printed out, and the certificate window is handled by the autoIt code I have in the selectCert() function.
I have tried clicking on different elements on the screen, and so far this bug only occurs when clicking on elements that have an xpath with /img at the end of it. Not sure if that helps at all... Any help would be appreciated!

Determine popup/new window type so I can interact with it in selenium webdriver

This may be a noob question, but I don't know how to determine what type of popup I am interacting with so I can have enough information to automate it using selenium webdriver.
I assumed it was just a popup, but when using (what seems to be) the standard way of dealing with popups my Selenium JUnit test freezes the instance the button is clicked.
String mainWindowHandle = driver.getWindowHandle();
//P2: Click on the element that opens the popup
driver.findElement(By.xpath("//a[contains(text(),'Login')]")).click();
Set s=driver.getWindowHandles();
Iterator ite=s.iterator();
while(ite.hasNext()){
System.out.println("Current ite = " + ite.next().toString());
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mainWindowHandle)){
driver.switchTo().window(popupHandle);
System.out.println("reached popup window? Now perform the login procedure before returning to the original window");
driver.switchTo().window(mainWindowHandle);
}
}
This code was described here and was the accepted answer. I have also seen many other similar examples of this code, however in my application the selenium test hangs on line
driver.findElement(By.xpath("//a[contains(text(),'Login')]")).click();
It just waits for me to enter login info, and until I do that manually the test hangs. If I do enter the info the test continues. I want to have this code finding the handle of the popup window (and eventually) populating it with username and pass and clicking on 'ok'. Though could it be possible that I am not dealing with a traditional popup? How could I tell?
This login button is the following element in the html Login
I faced the same issue while using selenium RC. I have used try catch block as shown below which helped me in solving the problem.
hope it will be useful.
//webdriver code till clicking the pop up link
try {
selenium.waitForPopUp("myWindow","3000");
}
catch(Exception e){
selenium.selectPopUp("myWindow");
selenium.windowFocus();
//activity that needs to be performed on the pop up window***
//code that takes back the focus to main window
String[] winFocus;
String winTitle;
winFocus = selenium.getAllWindowTitles();
winTitle = winFocus[0];
selenium.selectWindow(winTitle);
}