The button pressing command doesn't work. It's finding the button, but isn't clicking the button. When clicking the button there should be a native page that opens within Gmail.
All the code below is attempting to click the button within the new contacts page of Gmail https://mail.google.com/mail/u/0/1#contact/new
Inspecting the element the div tag is div tabindex="0" aria-label="Email" data-tooltip="Email" aria-disabled="false" style="-moz-user-select: none;" id=":2l" class="T-I J-J5-Ji T-I-ax7 T-I-Js-IF L3" role="button">div class="J-J5-Ji T-I-J3 Nz NS">/div>/div>
System.out.println("Finding Button");
driver.findElement(By.id(":2l")).click();
System.out.println("printing button");
System.out.println(driver.findElement(By.id(":2l")));
System.out.println("Finding button 2");
WebElement composeBtn = driver.findElement(By.cssSelector("div[class='T-I J-J5-Ji T-I-ax7 T-I-Js-IF L3']"));
System.out.println("Clicking button 2");
composeBtn.click();
System.out.println("Button 2 Clicked");
System.out.println(composeBtn.toString());
System.out.println("Finding button 3");
WebElement cBtn = driver.findElement(By.cssSelector("div[class= 'J-J5-Ji T-I-J3 Nz NS']"));
System.out.println("Clicking button 3");
cBtn.click();
Please let me know if you can help me identify this button
When I look at that page in the link you have provided, the compose button is grayed out and is not clickable. Having a program trying to click a button that a user could not click is still going to fail. Selenium will not and can not interact with objects that a user could not interact with (such as hidden fields, and in this case, grayed out buttons).
This method uses contains.
package testCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class GmailFileUpload
{
WebDriver driver = null;
WebElement element = null;
#Before
public void setUp() throws Exception
{
File file = new File("G:\\Selenium\\All_Jars\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
driver.manage().window().maximize();
}
#Test
public void test() throws InterruptedException, AWTException
{
driver.get("https://www.google.co.in");
driver.findElement(By.linkText("Sign in")).click();
driver.findElement(By.id("Email")).sendKeys("aavinashpande#gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("signIn")).click();
driver.findElement(By.linkText("Gmail")).click();
Thread.sleep(5000);
//click on compose
//driver.findElement(By.xpath("//div[#class='T-I J-J5-Ji T-I-KE L3'] ")).click();
driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//textarea[#name='to']")).sendKeys("aavinashpande#gmail.com");
driver.findElement(By.xpath("//input[#name='subjectbox']")).sendKeys("aavinashpande#gmail.com");
Thread.sleep(5000);
element = driver.findElement(By.xpath("//div[#class='Ar Au']//div"));
element.click();
element.sendKeys("Hi Avinash");
Thread.sleep(3000);
}
#After
public void teardown() throws Exception
{
driver.quit();
}
}
I find the send button like this:
driver.FindElement(By.XPath("//div[contains(text(),'Send')]")).Click();
After that you can do a quit. Just have an extra pop up ask you to confirm leaving the account:
driver.Navigate().GoToUrl("https://mail.google.com/mail/logout?hl=en");
I have sent an Emil successfully through selenium automation using Gmail Account with the below script.
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://www.google.co.in/";
selenium = new WebDriverBackedSelenium(driver, baseUrl);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[#id=':jb']/div[#class='z0']/div")).click(); // Compose
selenium.type("//div[#class='wO nr l1']//textarea[#name='to']", "vikramn#gmail.com"); // For To
selenium.type("//div[#class='aoD az6']//input[#name='subjectbox']", "Wanted to SAY HI"); // For Subject
selenium.type("//div[#class='Ar Au']/div[#class='Am Al editable LW-avf']", "Hi Vikram");// For Message body
selenium.click("//div[#class='J-J5-Ji']/div[#class='T-I J-J5-Ji aoO T-I-atl L3']"); //send
You can use this code to compose email using selenium web driver for gmail
public void gmail() {
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#aria-label='Email or phone']")).sendKeys("Your email");
driver.findElement(By.xpath("//span[.='Next']")).click();
//wait.until(ExpectedConditions.elementToBeClickable(password));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.xpath("//input[#aria-label='Enter your password']")).sendKeys("your password");
driver.findElement(By.xpath("//span[.='Next']")).click();
driver.findElement(By.xpath("//div[contains(text(),'Compose')]")).click();
}
driver.findElement(By.xpath("//*[#role='button' and text()='Compose']")).click();
Related
I have written a Java code for selenium and I'm trying to change the country on my website using selenium, somehow the code is running fine and the steps are executed as per the command line but when I check the execution of the test, the country isn't changed.
This is the code i have written
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class simpleCartFlow {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","/Users/manavmehta/Desktop/squareoffSeleniumProjects/chromedriver");
WebDriver driver=new ChromeDriver();
driver.get("http://release.squareoffnow.com/");
driver.manage().window().setSize(new Dimension(1440, 789));
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// selectCountry.usaCountry(driver);
System.out.println("step 1 start");
driver.findElement(By.cssSelector(".selected-flag > .iti-flag")).click();
driver.manage().timeouts().implicitlyWait(60000, TimeUnit.MILLISECONDS);
driver.findElement(By.xpath("(//li[#id='iti-item-us'])")).click();
System.out.println("flag clicked pleaseeeeeeeee");
System.out.println("step 1 done");
driver.findElement(By.linkText("Products")).click();
// driver.quit();
driver.findElement(By.cssSelector(".store-buy-pro-button")).click();
driver.findElement(By.cssSelector(".pro-twinpack-button")).click();
driver.findElement(By.cssSelector(".whole-purchase-button")).click();
driver.findElement(By.cssSelector(".productAvailability > .click-button")).click();
driver.findElement(By.cssSelector(".giftpackSubmit")).click();
System.out.println("button not clicked");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.cssSelector(".checkout-anchor")).click();
System.out.println("button clicked");
details.addressDetails(driver);
// driver.findElement(By.id("name1")).click();
// driver.findElement(By.id("name1")).sendKeys("tester");
// driver.findElement(By.id("name2")).sendKeys("tester");
// driver.findElement(By.id("phone1")).sendKeys("13024025552");
// driver.findElement(By.id("email1")).sendKeys("XYZ#GMAIL.COM");
// driver.findElement(By.id("address1")).sendKeys("--");
//// driver.findElement(By.id("state1")).sendKeys("California");
// driver.findElement(By.id("zipcode1")).sendKeys("12085");
// driver.findElement(By.id("city1")).sendKeys("Guilderland Center");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.cssSelector(".secure-pic")).click();
driver.switchTo().frame(1);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("Field-numberInput")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("Field-numberInput")).sendKeys("4242 4242 4242 4242");
driver.findElement(By.id("Field-expiryInput")).sendKeys("04 / 22");
driver.findElement(By.id("Field-expiryInput")).sendKeys("04 / 44");
driver.findElement(By.id("Field-cvcInput")).sendKeys("323");
// driver.quit();
}
}
what I want to do is click on the flag, and change the country from the dropdown.
I did a check to find if the element is present, the value came as true. I also added implicit wait of 30 seconds!!
the issue is that the element is not interactable
What should happen is that these textfields for login should be filled and the the login button should be pressed (some sort of auto login)
Here is the link to the webpage: Telekom Email Login Page
This are the three methods I'm using:
public String exportDriver() throws IOException {
final InputStream IEDriverStream = getClass().getResourceAsStream("/Driver/IEDriverServer.exe");
final File ieDriverServer = FileWebOpener.stream2file(IEDriverStream, "IEDriverServer", ".exe");
System.setProperty("webdriver.ie.driver", ieDriverServer.getAbsolutePath());
return ieDriverServer.getAbsolutePath();
}
public void goToWebsite(String url) {
driver = new InternetExplorerDriver();
driver.get(url);
}
public void setUsernameAndPassword(String username, String password, int Website) throws InterruptedException {
try{ new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#user[name='pw_usr']"))).sendKeys(username);
driver.findElement(By.cssSelector("input#pw_pwd[name='pw_pwd']")).sendKeys(password);
driver.findElement(By.cssSelector("input.button.standard_button_size.large#pw_submit")).click();
} catch(Exception e){
e.printStackTrace();
}
}
This is the exception I get
I believe you first need to :
click on the username field.
send username keys.
click on the password field.
send password keys.
click on the submit button
try- findElement(By.Class("line_normalized clear relative").click);
this find the username textfield class and clicks on it if Im not mistaken, syntax may be wrong but the idea is the same.
same goes for the password field, find the class/id name of the field & click on it before sending keys.
I have tested it with C# and get the right behaviors.
Below is the code:
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using System;
namespace IEWebDriver
{
class Program
{
static void Main(string[] args)
{
string username = args.Length > 1 ? args[1] : "test";
string password = args.Length > 1 ? args[2] : "test";
IWebDriver driver = new InternetExplorerDriver();
//IWebDriver driver = new ChromeDriver();
//Navigate to test URL
driver.Navigate().GoToUrl("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
try
{
new WebDriverWait(driver, new TimeSpan(0, 0, 20, 60)).Until(c => c.FindElement(By.CssSelector("input#user[name='pw_usr']"))).SendKeys(username);
driver.FindElement(By.CssSelector("input#pw_pwd[name='pw_pwd']")).SendKeys(password);
driver.FindElement(By.CssSelector("input.button.standard_button_size.large#pw_submit")).Click();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
//Close the browser
driver.Quit();
//driver.Close();
}
}
}
Hi as you want to enter the username and it's a input field, you need to locate the input tag.
you can try the following locator to locate the text input area and perform the sendkeys operation:
driver.findElement(By.xpath("//div[#class='line_normalized clear relative']/input")).sendKeys("username");
I suspect your application is too slow.Use WebDriverWait to identify the element and then Sendkeys operation.
package pkg1;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class testIE {
public static void main(String[] args) {
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir") + File.separator + "\\Executables\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement user= wait.until(ExpectedConditions.elementToBeClickable(By.id("user")));
user.sendKeys("user12345#gmail.com");
WebDriverWait wait1 = new WebDriverWait(driver, 30);
WebElement pass= wait1.until(ExpectedConditions.elementToBeClickable(By.id("pw_pwd")));
pass.sendKeys("password");
}
}
Output
As you are accessing the website initially you need to induce WebDriverWait for the desired elements to be clickable and you can use either of the following Locator Strategies:
cssSelector:
driver.get("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#user[name='pw_usr']"))).sendKeys("Jannik");
driver.findElement(By.cssSelector("input#pw_pwd[name='pw_pwd']")).sendKeys("Jannik");
driver.findElement(By.cssSelector("input.button.standard_button_size.large#pw_submit")).click();
xpath:
driver.get("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='user' and #name='pw_usr']"))).sendKeys("Jannik");
driver.findElement(By.xpath("//input[#id='pw_pwd' and #name='pw_pwd']")).sendKeys("Jannik");
driver.findElement(By.xpath("//input[#class='button standard_button_size large' and #id='pw_submit']")).click();
Till yesterday the below mentioned code was working fine but now i am facing some problem .
The code opens firefox browser then loads facebook.com but the code is not sending
email and password to web browser i.e. sendkeys() is not working.
I verified the id of both textbox of email and password which are correct yet code is not working .
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
public class Webdriver2 {
WebDriver driver ;
JavascriptExecutor jse;
public void invokeBrowser()
{
try
{
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.19.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS );
driver.get("https://www.facebook.com/");
search();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void search()
{
try
{
driver.findElement(By.id("email")).sendKeys("example#gmail.com");
Thread.sleep(4000);
driver.findElement(By.id("pass")).sendKeys("password");
Thread.sleep(4000);
driver.findElement(By.id("u_0_2")).click();
Thread.sleep(4000);
/*driver.findElement(By.name("q")).sendKeys("spit mumbai");
Thread.sleep(4000);
driver.findElement(By.xpath(" //button[#aria-label='Search' and #data-testid='facebar_search_button'] ")).click();*/
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Webdriver2 w = new Webdriver2();
w.invokeBrowser();
}
}
Try clicking on the two textboxes before you are performing the sendKeys:
driver.findElement(By.id("email")).click();
driver.findElement(By.id("email")).sendKeys("example#gmail.com");
The textboxes probably needs focus.
We need to take care of a couple of things here as follows:
The Email or Phone field is within an <input> tag so we need to take it into account while selecting the locator.
The Password field is also within an <input> tag so we need to take it into account while selecting the locator.
If you observe closely the id of the Log In button is dynamic and also within an <input> tag, so we need to consider this factor as well.
Here is the minimum sample code block using cssSelector to access the url https://www.facebook.com/, provide Email or Phone and Password, finally click on Log In button:
package demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Facebook_Login_CSS_FF {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.findElement(By.cssSelector("input#email")).sendKeys("Ryusei");
driver.findElement(By.cssSelector("input[name='pass']")).sendKeys("Nakamura");
driver.findElement(By.cssSelector("input[value='Log In']")).click();
}
}
I am trying to automate salesforce native apps create contact page. I am able to click and input text in all field which are seen on android mobile's first page. But for other fields which I get after scrolling down the page, appium is able to find the field as seen from appium log, but while clicking and sending text, it always send to last edited textbox on first page.
Can please anyone let me know If there is something extra needed to send text after scrolling page with appium.
I am using Android samsung galaxy S3 device. Below is the code and UIAutomator screen shot.
package samsungGalaxy;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
public class FirstTest {
AndroidDriver driver;
#BeforeTest
public void setUp() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "ce1d12134");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
capabilities.setCapability(CapabilityType.VERSION, "4.4.2");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "com.salesforce.chatter");
capabilities.setCapability("appActivity", "com.salesforce.chatter.Chatter");
capabilities.setCapability("appium-version", "1.4.16.1");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
#Test
public void login() {
driver.findElement(By.xpath("//android.widget.ImageView[contains(#resource-id,'home')]")).click();
driver.findElement(By.xpath("//android.widget.TextView[#text='Contacts']")).click();
driver.findElement(By.id("com.salesforce.chatter:id/new_button")).click();
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Create Contact Heading')]/..//android.view.View[contains(#content-desc,'Phone')]/../android.widget.EditText")).click();
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Create Contact Heading')]/..//android.view.View[contains(#content-desc,'Phone')]/../android.widget.EditText")).sendKeys("5109651200");
driver.hideKeyboard();
driver.scrollTo("Mobile");
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Create Contact Heading')]/..//android.view.View[contains(#content-desc,'Mobile')]/../android.widget.EditText")).click();
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Create Contact Heading')]/..//android.view.View[contains(#content-desc,'Mobile')]/../android.widget.EditText")).sendKeys("6509651200");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#AfterTest
public void End() {
driver.closeApp();
driver.quit();
}
}
I tried to just send the text to Mobile field using TouchAction also as below. In appium log it says success but on moble page neither click nor input happens.
driver.swipe(200, 1140, 250, 600, 4000);
WebElement we = driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Mobile')]/..//android.widget.EditText"));
TouchAction touchAction = new TouchAction(driver);
touchAction.press(we);
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
boolean displayed = we.isDisplayed();
System.out.println("Displayed :" + displayed);
we.click();
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Mobile')]/..//android.widget.EditText")).sendKeys("6509651200");
I guess you are specifying the same locator to send the keys after scrolling to an element as well. The statement
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Create Contact Heading')]/..//android.view.View[contains(#content-desc,'Mobile')]/../android.widget.EditText")).sendKeys("5109651200"); // or replace `Mobile` by `Phone`
is locating the same android.widget.EditText which probably is because of the XPath syntax you have used /../. Quoting from w3schools, the Xpath syntax is as follows :
nodename Selects all nodes with the name "nodename"
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
# Selects attributes
So you might want to change your code as :
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Phone')]../android.widget.EditText")).sendKeys("5109651200");
driver.hideKeyboard();
driver.scrollTo("Mobile");
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Mobile')]../android.widget.EditText")).sendKeys("6509651200");
Note : In your case ../ and ..// both shall work because of the
hierarchy of android classes in the app.
User your code like below:
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Create Contact Heading')]//android.view.View[contains(#content-desc,'Phone')]//android.widget.EditText")).click();
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Create Contact Heading')]//android.view.View[contains(#content-desc,'Phone')]//android.widget.EditText")).sendKeys("5109651200");
driver.hideKeyboard();
driver.scrollTo("Mobile");
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Create Contact Heading')]//android.view.View[contains(#content-desc,'Mobile')]//android.widget.EditText")).click();
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Create Contact Heading')]//android.view.View[contains(#content-desc,'Mobile')]//android.widget.EditText")).sendKeys("6509651200");
I have used the code below trying to open same multiple window "Google". Kindly help me in editing this and explaining how to handle this .
driver.switchTo().window("gbar");//not sure how to use this
and below code tried in Selenium:
package Testing;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import junit.framework.*;
public class Float {
public static void setUp() {
WebDriver driver = new FirefoxDriver();
driver.navigate().to("https://www.google.com");
driver.manage().window().maximize();
}
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.navigate().to("https://www.google.com");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.name("q"));
element.click();
WebDriverWait wait = new WebDriverWait(driver, 80);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));
element.sendKeys("hi");
element.clear();
Thread.sleep(2000);
element.sendKeys("hey");
element.submit();
setUp();
driver.switchTo().window("gbar");// //* not sure how to use this *///
WebElement element1 = driver.findElement(By.name("q"));
element1.click();
element1.sendKeys("hi");
element1.clear();
element1.sendKeys("hey");
element1.submit();
driver.quit();
}
}
You can get a handle to your window via driver.getWindowHandle()and you can switch to a window with driver.switchTo().window("handle");.
If you want to open a new window you can click on a link with target="_blank" on the website or execute JavaScript to open a new window. Then you'll find another handle in driver.getWindowHandles(). A possible way could be:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
List<String> knownHandles = new ArrayList<String>();
knownHandles.add(driver.getWindowHandle());
((JavascriptExecutor)driver).executeScript("window.open();");
// find the new handle. we are getting a set
for (String handle : driver.getWindowHandles()) {
if (!knownHandles.contains(handle)) {
knownHandles.add(handle);
break;
}
}
String newHandle = knownHandles.get(knownHandles.size() -1 );
driver.switchTo().window(newHandle);
driver.get("https://www.google.com");
Another way is to inject the anchor and click it via JavaScript.
//Store the current window handle
String winHandleBefore = driver.getWindowHandle();
//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
driver.manage().window().maximize();
//Close the new window, if that window no more required
driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
Here is a sample example handling multiple windows:
public class Mytesting {
WebDriver driver = new FirefoxDriver();
#Before
public void beforetest() {
driver.manage().window().maximize();
driver.get("http://only-testing-blog.blogspot.in/2014/01/textbox.html");
}
#Test
public void test () throws InterruptedException
{
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();
// Get and store both window handles in array
Set<String> AllWindowHandles = driver.getWindowHandles();
String window1 = (String) AllWindowHandles.toArray()[0];
System.out.print("window1 handle code = "+AllWindowHandles.toArray()[0]);
String window2 = (String) AllWindowHandles.toArray()[1];
System.out.print("\nwindow2 handle code = "+AllWindowHandles.toArray()[1]);
//Switch to window2(child window) and performing actions on it.
driver.switchTo().window(window2);
driver.findElement(By.xpath("//input[#name='fname']")).sendKeys("My Name");
driver.findElement(By.xpath("//input[#value='Bike']")).click();
driver.findElement(By.xpath("//input[#value='Car']")).click();
driver.findElement(By.xpath("//input[#value='Boat']")).click();
driver.findElement(By.xpath("//input[#value='male']")).click();
Thread.sleep(5000);
//Switch to window1(parent window) and performing actions on it.
driver.switchTo().window(window1);
driver.findElement(By.xpath("//option[#id='country6']")).click();
driver.findElement(By.xpath("//input[#value='female']")).click();
driver.findElement(By.xpath("//input[#value='Show Me Alert']")).click();
driver.switchTo().alert().accept();
Thread.sleep(5000);
//Once Again switch to window2(child window) and performing actions on it.
driver.switchTo().window(window2);
driver.findElement(By.xpath("//input[#name='fname']")).clear();
driver.findElement(By.xpath("//input[#name='fname']")).sendKeys("Name Changed");
Thread.sleep(5000);
driver.close();
//Once Again switch to window1(parent window) and performing actions on it.
driver.switchTo().window(window1);
driver.findElement(By.xpath("//input[#value='male']")).click();
Thread.sleep(5000);
}