Selenium WebDriver, TestNG with Java - java

I am unable to Handle the Print window/popup when I click on a Print button on the Web application. I need to be able to either Close this Window, Click on Print or Cancel. I am not sure whether this is a pop-up or a window.
Could some one help me?

see whether any web elements are visible if you hover the mouse over the popup.
if web elements are visible then its web application pop up
if no web elements are visible then its windows popup
However you can ignore the popup by sending escape key. the following code will work. i just tried, it worked.
public class demo extends parent {
WebDriver driver = new FirefoxDriver();
#Test
public void launch() throws InterruptedException {
driver.get("https://www.google.co.in");
Robot r = null;
try {
r = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement el = driver.findElement(By.xpath(".//*[#id='hplogo']"));
el.click();
Thread.sleep(10000);
el.sendKeys(Keys.CONTROL + "p"); // trying to invoke print pop up
Thread.sleep(10000);
r.keyPress(KeyEvent.VK_ESCAPE); //dismissing it by sending escape keys
}
}
hope you will get some idea here :)

Related

How can I click a floating advertisement?

I'm trying to move to a different page (click 'my account'), and a floating advertisement appears:
advertisement
I tried to click on it, and can't find the "Close" element.
Found that it might related to frames, but still not works.
My Code:
public void clickMyAccount() {
driver.findElement(myAccountBtn).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void clickCloseAd(){
driver.switchTo().frame(driver.findElement(By.id("google_esf")));
driver.switchTo().frame(driver.findElement(By.id("aswift_9")));
driver.switchTo().frame(driver.findElement(By.id("ad_iframe")));
driver.findElement(By.id("dismiss-button")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
driver.switchTo().defaultContent();
}
Site:
https://practice.automationtesting.in/
Any idea how can I click the floating advertisement?
Tried: move between frames until able to find the Close element
Actual: still can't find this element
It is hard to handle advertisement popUp. So we can handle it in 2 ways:
downloading ad-blocker extension to your chrome and using the chrome option code.
download ad-bocker- https://chrome.google.com/webstore/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom
use chromeoption driver code:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("disable-infobars");
Notice that popUp gets disabled is do back. so we can is click on myaccount go back and then click back:
WebElement MyAccount = driver.findElement(By.xpath("//[#href='https://practice.automationtesting.in/my-account/']"));
MyAccount.click();
driver.navigate().back();
MyAccount.click();
Found the issue:
My 'wait' not always working, so the driver tries to close the ad when not displayed - still searching for a way to correct this
It's working without frame1: "google_esf", only frame2 and frame3 needed
Updated code:
public void clickMyAccount() {
System.out.println("Click My Account");
if(driver.findElement(myAccountBtn).isDisplayed()){
driver.findElement(myAccountBtn).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
} else {
System.out.println("My account button not displayed");
}
}
public void clickCloseAd(){
System.out.println("Click Close Ad");
if(driver.findElement(By.id("aswift_9")).isDisplayed()){
// driver.switchTo().frame(driver.findElement(By.id("google_esf"))); //Not in use but also a frame
driver.switchTo().frame(driver.findElement(By.id("aswift_9")));
driver.switchTo().frame(driver.findElement(By.id("ad_iframe")));
driver.findElement(By.id("dismiss-button")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
driver.switchTo().defaultContent();
} else {
System.out.println("Ad not displayed");
}
}
Thanks all!

not able to click on button after the pop-up appears in appium

I have used appium 1.6.4 and android version is on 5.1 ,6.1 and 7.0
Below is my code
public boolean navigateToLoginPage(){
String strng = null;
newObj = new LogInPage();
wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOf(GetStarted));
GetStarted.click();
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
if(Country.isDisplayed()){
Country.click();
wait.until(ExpectedConditions.visibilityOf(Cancel));
Cancel.click();
}
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
MobileNumber.sendKeys("91**********");
Next.click();
driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
OK.click();// this is the problem , this sometimes works sometimes not
//i have used the alert interface but no use
//if(OK.isEnabled()){OK.click();} it returns true and I have also used .isDisplayed but no use
//When Ok button is not clicked but the code in the try block is executed
//I have also used the tap functionality but no effect
try {
strng =newObj.try1();
} catch (SQLException e) {
e.printStackTrace();
}
wait.until(ExpectedConditions.visibilityOf(EnterOTP));
EnterOTP.sendKeys(strng);
Next.click();
return true;
}

When I run a selenium test for Firefox to create a user the test runs but I never see the user in my list

However, if I set up to run using Microsoft Edge the test completes and i can see the user just created in the list. Here is my code. Very confused.
As you can see all I change to run for Microsoft Edge is un-comment the setProperty line and then change the FirefoxDriver to EdgeDriver. When I do that as I said the script runs and upon completion I log in and can see the user in the list while when using this code I cannot see the user.
public static void main (String[] args) throws InterruptedException {
// System.setProperty("webdriver.edge.driver", "/Program Files (x86)/Microsoft Web Driver/MicrosoftWebDriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
baseUrl = "http://briotest.brio.viddler.com/";
firstName = "Sam";
lastName = "Bradford";
email = "sbradford#mail.com";
password = "Sooners1!";
test();
}
public static void test() throws InterruptedException {
// get to login page and enter credentials
driver.get(baseUrl + "/users/login");
driver.findElement(By.name("email_address")).clear();
driver.findElement(By.name("email_address")).sendKeys("jfayefrank#yahoo.com");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("*********");
driver.findElement(By.cssSelector("button.button")).click();
// Now on the assets view page. Go to Users and select Create
driver.findElement(By.xpath("/html/body/header/div[2]/nav/div/ul[1]/li[6]/a")).click(); // Users tab
driver.findElement(By.linkText("Create")).click();
// Enter new users credentials and submit
driver.findElement(By.id("email")).clear();
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("first_name")).clear();
driver.findElement(By.name("first_name")).sendKeys(firstName);
driver.findElement(By.name("last_name")).clear();
driver.findElement(By.name("last_name")).sendKeys(lastName);
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys(email);
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys(password);
try {
Thread.sleep(5000);
}
catch (Exception e) {
System.out.println("Could not perform pause");
}
// driver.findElement(By.cssSelector("input.button")).click();
driver.findElement(By.xpath("/html/body/div[1]/section/div/article/form/input[2]")).click();
System.out.println ("User created!!!");
}
Problem solved. Thanks to a coworker my problem is resolved. Apparently since Firefox was not maximized the Submit button was not really selected even though it appeared to do the action. Once I added driver.manage().window().maximize(); my users were now visible in the list. Did I mention the coworker was an intern...

When will the java.awt.Robot fail to click?

I use java.awt.Robot to perform some mouse behaviors on my PC. The code is simple like below:
import java.awt.Robot;
import java.awt.event.InputEvent;
public class RobotProxy {
public static void main(String[] args) {
// TODO Auto-generated method stub
RobotProxy robotProxy = new RobotProxy();
try {
robotProxy.foo();
} catch (Exception e) {
// TODO: handle exception
System.out.println("Exception there...");
}
}
public void foo() throws Exception{
Thread.sleep(3000);
Robot robot = new Robot();
robot.mouseMove(501, 296);
leftClick(robot);
robot.mouseMove(505, 296);
leftClick(robot);
robot.mouseMove(509, 296);
leftClick(robot);
}
public void leftClick(Robot robot) throws Exception{
Thread.sleep(1000);
System.out.println("before Click...");
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
System.out.println("after Click...");
}
}
You can find that I use the combination of java.awt.Robot.mousePress(InputEvent.Button1_MASK) and java.awt.Robot.mouseRelease(InputEvent.Button1_MASK) to perform the mouse left click behavior.
It works fine at most time but fails sometimes. For example, the left click behavior for a kind of software's check box will fail. I can make sure I send the click command to java.awt.Robot but just nothing happens. What's more incredible is that java.awt.Robot.mouseMove(int x, int y) still works in that situation.
PC's OS is Windows8.1
The software is not market available and it's just a Windows native app written by cpp. The button on the software can be clicked but not for check box.
If the situation makes you confused, pls just tell me when will the java.awt.Robot fail to click. Thanks for your help in advance.
The problem is there is no delay between the robot.mousePress and robot.mouseRelease commands.
Here is an example of what you could add in between the two to fix the issue
Thread.sleep(100); a delay for 100ms. about as fast as you can hear, click click

selenium webdriver modal dialog java

I am testing my form and when I don't type needed data I get javascript alert in my web app that tells the user to enter missing data. I can't handle this with selenium because when I partially fill form and try to submit I get exception
org.openqa.selenium.UnhandledAlertException: Modal dialog present
If I catch exception the alert in webdriver is not shown. Is that any solution to solve this issue?I would like to be able to submit form and catch the alert. I am using Linux Mint,Firefox 18 and selenium 2.28.0 with java
Best regards
UPDATE
I have following in my code
somePage.fillName(sth); //only 1 of 2 required field are filled
somgePage.submit(); //here js alert is shown right after clicking submit
somePage.getCurrentAlert();
//here are code parts
public Alert getCurrentAlert(){
return driver.switchTo().alert();
}
public AdminHome submit(){
saveUrl();
WebElement submit = driver.findElement(By.id("add_quiz_submit_button"));
try{
submit.click();
if(urlChanged()){
return new AdminHome(driver);
}
}
catch(Exception e){
e.printStackTrace();// exception 1
return null;
}
return null;
}
//Exception 1
org.openqa.selenium.UnhandledAlertException: Modal dialog present
//The test fails because of:
org.openqa.selenium.NoAlertPresentException: No alert is present (WARNING: The server did not provide any stacktrace information)
However if I click manual on submit the test work as expected. Thanks in advance
you should handle the alert as soon as the action is done and there shouldn't be any other action before handling the alert.
for instance your code should be
try{
submit.click();
if (alertPresent())
getCurrentAlert();
if(urlChanged()){
return new AdminHome(driver);
}
}
This will check alert and then accept the alert. The interaction of webdriver is more similar to the action we interact with manually with browser. So when the click on submit is done we will be able to see alert and no actions can be done until accept or reject it.
Vishal
It is because driver accepts the alert itself when the UnhandledAlertException is thrown. How can you submit the form if you have filled it partially?
If it is even possible, just catch that exception, and in catch block write the line which clicks on the submit button.
Use Robot class (Press enter) to close modal dialog box
try {
(new Robot()).keyPress(java.awt.event.KeyEvent.VK_ENTER);
(new Robot()).keyRelease(java.awt.event.KeyEvent.VK_ENTER);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Categories

Resources