The setValue method putting value in the field too fast and losing some chars during a process.
sendKeys method doesn't work correctly too.
[https://pp.userapi.com/c849532/v849532534/1d4fe6/BEVb5_C3O8E.jpg][]
Appium Server 1.13.0
package appiumtests2;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
import static java.lang.Thread.sleep;
public class Stoloto {
/*
* static WebDriver driver;
* AndroidDriver driver;
*/
static AppiumDriver<MobileElement> driver;
public static void main(String[] args) {
try {
Stoloto stoloto = new Stoloto();
stoloto.openStoloto();
} catch (Exception exp) {
System.out.println(exp.getCause());
System.out.println(exp.getMessage());
exp.printStackTrace();
}
}
#Test
public void openStoloto() throws Exception {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "Pixel XL");
cap.setCapability("udid", "HT6B70200690");
cap.setCapability("platformName", "Android");
cap.setCapability("platformVersion", "9");
cap.setCapability("appPackage", "ru.stoloto.mobile");
cap.setCapability("appActivity", "ru.stoloto.mobile.ca.presentation.ui.activity.MainActivity");
URL url = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AppiumDriver<MobileElement>(url, cap);
System.out.println("Application Started...");
MobileElement onBoardContinue = driver.findElement(By.id("ru.stoloto.mobile:id/confirm"));
onBoardContinue.click();
onBoardContinue.click();
MobileElement onBoardLogin = driver.findElement(By.id("ru.stoloto.mobile:id/login"));
onBoardLogin.click();
MobileElement loginField = driver.findElement(By.id("ru.stoloto.mobile:id/user"));
String login = "testtesttest#gmail.com";
sleep(1000);
loginField.setValue(login);
MobileElement passwordField = driver.findElement(By.id("ru.stoloto.mobile:id/passwordInputEdit"));
String password = "qwertyqwerty";
sleep(1000);
passwordField.setValue(password);
driver.hideKeyboard();
MobileElement log_in = driver.findElement(By.id("ru.stoloto.mobile:id/buttonSubmit"));
log_in.click();
System.out.println("Test Completed");
}
}
I need to find a way to set value with a little delay or the other way, which help me to solve this problem.
You can use mobile:shell command for sending text to the device via adb shell
Map<String, Object> argv = new HashMap<>();
argv.put("command", "input");
argv.put("args", Lists.newArrayList("text", "your_text_here"));
driver.executeScript("mobile: shell", argv);
Alternatively you can consider using elementSendText function avaiable via Appium SeeTest Extension
driver.executeScript("seetest:client.elementSendText(\"NATIVE\", \"id=your_element_id\", \"0\", \"your_text_here\")");
try to use WebDriverWait in order to set the value
Related
I was trying to learn Selenium , i used flipkart site to automate , but when i redirect to flipkart i cant able to close a pop up
I used the 'x' button by its xpath & classname but its not working
[img]https://i.ibb.co/7bhDJYw/Screenshot-2022-11-29-at-10-31-19-AM.png[/img]
package ui;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Popup {
public static String browser = "Firefox";
public static FirefoxDriver driver;
public static void main(String args[]) throws InterruptedException {
if (browser.equals("Firefox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
} else if (browser.equals("Chrome")) {
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();
} else if (browser.equals("Edge")) {
WebDriverManager.edgedriver().setup();
EdgeDriver driver = new EdgeDriver();
}
driver.manage().window().maximize();
driver.get("https://www.flipkart.com/");
// Thread.sleep(5000);
// driver.findElement(By.className("_2KpZ6l _2doB4z")).click();
Thread.sleep(2000);
// Alert alert = (Alert) driver.switchTo().alert();
// alert.dismiss();
}
}
this is the code i tried
Instead of doing this
driver.findElement(By.className("_2KpZ6l _2doB4z")).click();
do this
driver.findElement(By.xpath("//button[contains(text(),'✕')]")).click();
or
driver.findElement(By.cssSelector("._2KpZ6l._2doB4z")).click();
I am trying to run some basic tests using Selenium Grid and I acquired the code below from my instructor.
However RemoveWebDriver driver is always NULL as device is always param-value-not-found
I am unable to understand what is setting the value of the parameter device and how to correct this error.
My knowledge of TestNG is limited. I am guessing TestNG is responsible for calling the openBrowser function which has a parameter device the value of this parameter is always param-value-not-found.
Therefore in the if else block device does not equal pc1 or pc2 which causes driver vairable to have a value Null.
I am unable to figure out how the value of device is getting derived.
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class gridTest
{
public static WebDriver driver ;
#Parameters("System")
#Test(priority=0)
public void openBrowser(String device) throws MalformedURLException // device is always param-value-not-found
{
System.out.println("device is : " + device);
if (device.equalsIgnoreCase("pc1"))
{
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability("browserVersion", "80");
driver = new RemoteWebDriver(new URL("http://192.168.1.6:30032/wd/hub"), cap);
}
else if (device.equalsIgnoreCase("pc2"))
{
DesiredCapabilities cap = DesiredCapabilities.firefox();
driver = new RemoteWebDriver(new URL("http://192.168.1.6:30032/wd/hub"), cap);
// adding the following else block merely bypasses the issue
} else {
String nodeUrl = "http://192.168.1.6:30032/wd/hub" ;
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL(nodeUrl), capabilities);
}
}
#Test(priority = 1)
public void Moda()
{
try
{
//Navigate to URL
driver.get("https://www.flipkart.com/");
driver.manage().window().maximize();
}
catch (Exception e)
{
System.out.println(e);
return;
}
// Close login modal screen
driver.findElement(By.xpath("//*[#class='_2AkmmA _29YdH8']")).click();
// Hover the menu Electronics >> MI
WebElement men1 = driver.findElement(By.xpath("//li[#class='Wbt_B2 _1YVU3_'][1]"));
Actions act = new Actions(driver);
act.moveToElement(men1).perform();
Thread.sleep(2000);
WebElement men2 = driver.findElement(By.xpath("//li[#class='_1KCOnI _3ZgIXy'][1]//a[contains(#href,'Electronics_0_Mi')]"));
men2.click();
//Verify 'Latest from MI' label on the search result page
try
{
Thread.sleep(3000);
}
catch(InterruptedException ie){
System.out.println("Error at line 76") ;
}
boolean Validate = driver.findElement(By.xpath("//p[text()='Latest from MI : ']")).isDisplayed();
System.out.println("Latest from MI element is displayed--" + Validate);
System.out.println("Completed.") ;
}
}
This means that your test expects to have System parameter value in your testng.xml file. You have to either add one or add #Optional annotation to your method parameter so that the parameter would have been set to that optional value if no values are detected in testng.xml.
See details here.
I have 2 packages, 1st the Base package package ceccms.automation.framework and another package package ceccms.testcases as follows:
package ceccms.automation.framework;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class UniversalMethods {
public static WebDriver driver = null;
public static String chromepath = "D:\\Automation\\Web Drivers\\ChromeDriver\\chromedriver.exe";
public static String edgepath = "D:\\Automation\\Web Drivers\\EdgeDriver\\MicrosoftWebDriver.exe";
public WebDriver openBrowser (String browser, String url) {
if (browser != null) {
switch (browser) {
case "Mozilla":
driver = new FirefoxDriver();
driver.get(url);
break;
case "Chrome":
System.setProperty("webdriver.chrome.driver", chromepath);
driver = new ChromeDriver();
driver.get(url);
break;
case "Edge":
System.setProperty("webdriver.edge.driver", edgepath);
driver = new EdgeDriver();
driver.get(url);
break;
default:
System.out.println("Wrong Browser Name");
driver.quit();
}
}
driver.manage().window().maximize();
return driver;
}
}
and
package ceccms.testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import ceccms.automation.framework.UniversalMethods;
public class LoginTest {
public static String url = "https://test.ceccms.com/Login.aspx?";
static UniversalMethods U = new UniversalMethods();
public static void main(String[] args) throws InterruptedException {
String browserName = "Chrome";
U.openBrowser(browserName, url);
WebElement userName = driver.findElement(By.id("txtUserName"));
userName.sendKeys("pkumar");
WebElement password = driver.findElement(By.id("txtUserPass"));
password.sendKeys("PassMe33");
Thread.sleep(3000);
driver.quit();
}
}
I am running the code through the second package. I want to use one object driver across the package without using the notation U.driver.findElement() . How can I achieve that ?
One of the solutions, you can add Page Objects, where define Web elements and give your Driver as parameter to initiate Page object class with all web elements. This will allow you to work directly with elements.
You can use it, with this structure(By extending universal method):
package ceccms.testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import ceccms.automation.framework.UniversalMethods;
public class LoginTest extends UniversalMethods {
//You can access driver and method without using object reference
public static String url = "https://test.ceccms.com/Login.aspx?";
public static void main(String[] args) throws InterruptedException {
String browserName = "Chrome";
openBrowser(browserName, url);
WebElement userName = driver.findElement(By.id("txtUserName"));
userName.sendKeys("pkumar");
WebElement password = driver.findElement(By.id("txtUserPass"));
password.sendKeys("PassMe33");
Thread.sleep(3000);
driver.quit();
}
}
I am starting to learn how to handle multiple tabs in a browser using Selenium with Java. looks like my code below is not working.
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class HandlingWindows {
public static void main(String[] args) throws InterruptedException
{
WebDriver driver= new FirefoxDriver();
driver.get("https://www.facebook.com/");
String parent= driver.getWindowHandle();
System.out.println("Parent Window is"+parent);
//Get Data Policy
WebElement we= driver.findElement(By.linkText("Data Policy"));
//Click Data Policy link
we.click();
//Create an arrayList
ArrayList<String> s1= new ArrayList<String>(driver.getWindowHandles());
for(String s2:s1)
{
if(!(s2.equalsIgnoreCase(parent)))
{
driver.switchTo().window(s2);
Thread.sleep(5000);
System.out.println(driver.getWindowHandle());
System.out.println("get title of window"+driver.getTitle());
}
}
}
}
Please let me know how can I display the title 'Data Policy' without using
getWindowHandles().
getWindowHandles() works pretty fine but before invoking getWindowHandles() you have to induce WebDriverwait as follows :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver= new FirefoxDriver();
driver.get("https://www.facebook.com/");
String parent= driver.getWindowHandle();
System.out.println("Parent Window is"+parent);
driver.findElement(By.linkText("Data Policy")).click();
WebDriverWait wait = new WebDriverWait(driver,5);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> s1= driver.getWindowHandles();
for(String s2:s1)
{
if(!parent.equalsIgnoreCase(s2))
{
driver.switchTo().window(s2);
Thread.sleep(5000);
System.out.println(driver.getWindowHandle());
System.out.println("get title of window"+driver.getTitle());
}
}
Console Output :
Parent Window is4294967297
4294967303
get title of windowData Policy
Just put Thread.sleep(3000); after clicking on Data Policy link.
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class HandlingWindows {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
//D:\new folde backup\Selenium_project\seleniumproject\testNG_practice\driver
System.setProperty("webdriver.gecko.driver", "./driver/geckodriver.exe");
driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
String parent = driver.getWindowHandle();
System.out.println("Parent Window is" + parent);
// Get Data Policy
WebElement we = driver.findElement(By.linkText("Data Policy"));
// Click Data Policy link
we.click();
// Create an arrayList
Thread.sleep(3000);
ArrayList<String> s1 = new ArrayList<String>(driver.getWindowHandles());
System.out.println(s1);
for (String s2 : s1) {
if (!(s2.equalsIgnoreCase(parent))) {
driver.switchTo().window(s2);
Thread.sleep(5000);
System.out.println(driver.getWindowHandle());
System.out.println("get title of window" + driver.getTitle());
}
}
}
}
Output:-
Parent Window is4294967297
[4294967297, 4294967301]
4294967301
get title of windowData Policy
Can seem to make this code execute. Compiler isn't instantiating the driver. what can I do to correct this.
package mypackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
org.openqa.selenium.support.ui.ExpectedConditions;
import
org.openqa.selenium.support.ui.WebDriverWait;
public class selenium {
public static void main(String[] args) {
WebDriver driver = (WebDriver) new FirefoxDriver();
WebDriverWait MyWaitlVar= new
WebDriverWait(driver, 10);
String baseUrl =
"http://newtours.demoaut.com";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = "";
// launch Firefox and direct it to the Base URL
driver.get(baseUrl);
// get the actual value of the title
actualTitle = driver.getTitle();
/*
* compare the actual title of the page
witht the expected one and print
* the result as "Passed" or "Failed"
*/
if (actualTitle.contentEquals
(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Firefox
driver.close();
// exit the program explicitly
System.exit(0);
}
}