How to handle pop up or notification in Macbook - selenium

I want to get text of pop up using selenium webdriver.
Open URL https://ads.google.com/intl/en_IN/home/pricing/
Click on link 1800-419-6346* ( I am having xpath for this driver.findElement(By.xpath("//a[#class='eto eto-number']")).click();)
Now after click on above link , Here one Pop up is getting displayed which is showing message as “Open FaceTime?” .
How we can handle such type of pop up , I just know “Open FaceTime” is application which run on MacBook .
I tried to handle with alert class but it did not work.
Alert alert=driver.switchTo().alert();
System.out.println(alert.getText());
after running code ,I am getting message "no such alert"
Please note on Windows machine that popup will not be displayed, it is specific to Macbook.
My Code as below :-
public class Session1Mac {
WebDriver driver;
#BeforeMethod
public void setUp() throws InterruptedException{
System.out.println("enter into first method");
System.setProperty("webdriver.chrome.driver", "/Users/aturkar/eclipse-workspace/AutomateBSQATaskonMac/Lib/chromedriver");
driver = new ChromeDriver(); // launch chrome
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https:/ads.google.com/intl/en_IN/home/pricing/");
Thread.sleep(20);
}
#Test()
public void VerifyRedirectingToDialer() throws InterruptedException, Exception{
// driver.findElement(By.linkText("800-419-6346*")).click();
driver.findElement(By.xpath("//a[#class='eto eto-number']")).click();
Thread.sleep(5000);
System.out.println("Element got clicked");
Alert alert=driver.switchTo().alert();
System.out.println(alert.getText());
}
}
Error on Console:-
Actual:-
org.openqa.selenium.NoAlertPresentException: no such alert
Expected:-I Want to get text of "Pop-up", if yes then how we can get it ? could you please help me with this.

Related

selenium.UnsupportedCommandException: the requested resource could not be found, or a request

getting exception
FAILED CONFIGURATION: #AfterClass tearDown
"org.openqa.selenium.UnsupportedCommandException: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource"
enter code here
public class BaseClass {
//read config file and initiate variables
ReadConfig readConfig = new ReadConfig();
public String username = readConfig.getUserName();
//public String password = "asas";
public String password = readConfig.getPassword();
public static AppiumDriver driver;
public static org.apache.logging.log4j.Logger logger;
#BeforeClass
public void setUp ()
{
try {
logger = LogManager.getLogger(BaseClass.class);
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(MobileCapabilityType.DEVICE_NAME, "bd178829");
dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
dc.setCapability(MobileCapabilityType.APP, "D:\\automation\\CRMNextMobileAutomation\\src\\test\\resources\\apps\\CRMNextNative 6.29.0-release_screenshot_enabled.apk");
dc.setCapability("automationName","UiAutomator2");
dc.setCapability("appPackage", "com.crmnextmobile.crmnextofflineplay");
dc.setCapability("appActivity", "com.crmnextmobile.crmnextofflineplay.qr.QrScannerActivity");
dc.setCapability("enforceAppInsall", true);
URL url = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AppiumDriver(url,dc);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
System.out.println("CRMNext automation start..");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
//Clicking on Allow option on open permission pop up
//driver.findElement(By.id("com.android.permissioncontroller:id/permission_allow_button")).click();
if(!driver.findElements(By.id ("com.android.permissioncontroller:id/permission_allow_button")).isEmpty()){
//THEN CLICK ON THE SUBMIT BUTTON
System.out.println("permission_allow_button is found on page");
driver.findElement(By.id("com.android.permissioncontroller:id/permission_allow_button")).click();
}else{
//DO SOMETHING ELSE AS SUBMIT BUTTON IS NOT THERE
System.out.println("permission_allow_button not found on page");
}
//Clicking on Allow button of run in background pop up
//driver.findElement(By.id("android:id/button1")).click();
if(!driver.findElements(By.id ("android:id/button1")).isEmpty()){
//THEN CLICK ON THE SUBMIT BUTTON
System.out.println("button1 is found on page");
driver.findElement(By.id("android:id/button1")).click();
}else{
//DO SOMETHING ELSE AS SUBMIT BUTTON IS NOT THERE
System.out.println("button1 not found on page");
}
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
Thread.sleep(5000);
System.out.println("CRMNext automation Before Skip..");
//Clicking on Skip button
driver.findElement(By.id("com.crmnextmobile.crmnextofflineplay:id/skip")).click();
System.out.println("CRMNext automation after Skip..");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Thread.sleep(10000);
driver.findElement(By.id("com.crmnextmobile.crmnextofflineplay:id/relative_layout_continue")).click();
Thread.sleep(2000);
} catch (Exception exp) {
// TODO: handle exception
System.out.println("Cause is :"+exp.getCause());
System.out.println("Message is :"+exp.getMessage());
exp.printStackTrace();
}
}
#Test
public void sample() {
System.out.println("Sample run");
}
#AfterClass
public void tearDown()
{
driver.close();
driver.quit();
}
//org.openqa.selenium.UnsupportedCommandException: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
all tests are failing due to this.
driver.close()
The driver.close() command is used to close the current browser window having focus. In case there is only one browser open then calling driver.close() quits the whole browser session.
Usability
Use driver.close() when dealing with multiple browser tabs or windows e.g. when clicking on a link that opens another tab. In this case after performing required action in the new tab, to close the tab, call the driver.close() method.
driver.quit()
The driver.quit() is used to quit the whole browser session along with all the associated browser windows, tabs and pop-ups.
Usability
Use driver.quit() when no longer want to interact with the driver object along with any associated window, tab or pop-up. Generally, it is the last statements of the automation scripts. Call driver.quit() in the #AfterClass method to close it at the end of the whole suite.
Use following code in #AfterClass
#AfterClass
public void tearDown()
{
if (driver != null)
driver.Quit();
}

I'm trying to execute automation code for http://site21.way2sms.com, error in 3rd & 4th line from last

I'm trying to execute automation code for http://site21.way2sms.com,
error in 3rd & 4th line from last
//error code
driver.findElement(By.xpath("//input[#name='mobile']")).sendKeys("xxxxxxxxx");
driver.findElement(By.xpath("//*[contains(#placeholder, 'Type your message ')]")).sendKeys("call me urgently,need to talk");
Tried code:-
package testpack;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test6 {
public static void main(String[] args) throws InterruptedException
{
// Run Chromedriver.exe to get chrome browser
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
ChromeDriver driver=new ChromeDriver();
Thread.sleep(5000);
//Launch site
driver.get("http://site21.way2sms.com/content/index.html");
Thread.sleep(5000);
driver.findElement(By.name("username")).sendKeys("xxxxxxxxxx");
driver.findElement(By.name("password")).sendKeys("xxxx");
Thread.sleep(5000);
driver.findElement(By.id("loginBTN")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//*[text()='Send SMS']")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//input[#name='mobile']")).sendKeys("xxxxxxxxxx");
driver.findElement(By.xpath("//*[contains(#placeholder, 'Type your message ')]")).sendKeys("Call me urgently, need to talk");
Thread.sleep(5000);
driver.findElement(By.name("Send")).click();
Try following code instead
driver.switchTo().frame(driver.findElement(By.id("frame")));
driver.findElementByXPath("//input[contains(#id,'mobile')]").sendKeys("xxxxxxxx");
driver.findElementByXPath("//*[#*='message'] | //*[#*='Type your message here or use SMS greetings']").sendKeys("test smsm");
driver.findElementByXPath("//*[#*='Send'] | //*[#*='return sendMessage();']").click();
You can use the below code :
driver.switchTo().frame("frame");
driver.findElement(By.id("mobile")).sendKeys("xxxxxxxxx");
driver.findElement(By.xpath("//*[contains(#placeholder, 'Type your message ')]")).sendKeys("Call me urgently, need to talk");
Thread.sleep(5000);
driver.findElement(By.name("Send")).click();
driver.switchTo().defaultContent();
your code is fine but you missed to switch to the frame.
enter image description here

Unable to handle a popup

I am trying to automate a website, however I am unable to handle the popup. I tried using switch to frame and also dismiss alert but both didn't work. Below is the code for same. Could someone please help me.
Code:
#Test(priority=1)
public void Overview() throws InterruptedException {
//driver.get(baseUrl);
logger = extent.startTest("Overview");
logger.log(LogStatus.INFO, "Launching the website >>> http://www.boxer.co.za/");
driver.get("http://www.boxer.co.za/");
Thread.sleep(2000);
driver.findElement(By.xpath("//a[#id='close-icon']")).click();
logger.log(LogStatus.INFO, "Click on Promotions");
driver.findElement(By.xpath("//a[#href='http://www.boxer.co.za/on-promotion/'][contains(.,'On Promotion')]")).click();
}
Try with below code and lets see if it works-
try{
driver.switchTo().frame(0);
driver.findElement(By.xpath("//a[#id='close-icon']")).click();
driver.switchTo().defaultContent();
}
catch (Exception e){}

Error: Unable to locate element: {"method":"xpath","selector":".//*[#id='billing:firstname']"}

Somebody please help me. I am simply trying to enter the text into the text box, but facing same error from a long time.
I have a constructor class and main class:
Unable to locate element:
{"method":"xpath","selector":".//*[#id='billing:firstname']"} Command
duration or timeout: 20.24 seconds
CODE:
#Test
public void FillFormPTC() throws InterruptedException {
SecondCat home= new SecondCat(driver);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// driver.switchTo().defaultContent();
// WebElement input = (new WebDriverWait(driver, 25)).until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[#id='billing:firstname']")));
home.TextFirstname("joe");
}

How to read alert message using Firefox Driver?

I need to read alert & confirmation messages displayed in pop ups using java and print it on the console. On export of the selenium recording from the IDE as a Junit4 (WebDriver) java file, my code is:
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
}
Now when I tried to use getAlert or getConfirmation functions as shown:
#Test
public void testSample() throws Exception {
Alert alert = driver.switchTo().alert();
message = alert.getText();
System.out.println("message is "+message);
}
I get the following error:
java.lang.NullPointerException
at com.example.tests.Sample.testSample(Sample.java:40)
at com.example.tests.Sample.main(Sample.java:149)
Exception: null
How do I handle this? Also is there any other way of reading the pop up messages?
In the testSample() method when you navigate to any page by using -
driver.get("URL");
After that can you explain how the alert message comes up in the 1st place.
Are you sure that the pop up message which appears is a javascript alert or any window which is opening up.
If it is a an alert message then you can access it by using -
driver.switchTo().alert();
But if the pop up is another window then you will have to use -
driver.switchTo().window("windowName");
You can get more information about this from here.