I'm trying to automate a app to do everything that you would have to do by hand. My main goal right now is to get it to click a button after logging into the app.
This is the very last line of code in my IDE
driver.findElement(By.id("com.offerup:id/main_text")).click();
After this line of code executes, OfferUp, the app that I'm testing on, closes. There are no failures in console but, I don't want it to close after that line of code executes.
When I log into the app without running my code, the app stays open but, when I run my code, it closes after driver.findElement(By.id("com.offerup:id/main_text")).click(); is executed.
Why this is happening?
Here is my full code -
package OpenOfferUpTest;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.server.handler.FindElement;
import org.testng.annotations.Test;
import org.testng.annotations.*;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
public class OpenOfferUp {
AndroidDriver driver;
#Test
public void OpensOfferUp() throws MalformedURLException
{
File OfferUp = new File("C:\\Users\\boung\\Desktop\\OfferUp.apk");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "Virtual Device");
cap.setCapability("platformName", "android");
cap.setCapability("null", "OfferUp");
cap.setCapability("appPackage", "com.offerup");
cap.setCapability("appActivity", "com.offerup.android.login.splash.LoginSplashActivity");
driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), cap);
}
#Test
public void SimpleTest() throws InterruptedException {
driver.findElement(By.id("com.offerup:id/email_button")).click();
By path = By.xpath("//*[#text='Enter your email address']");
driver.findElement(path).sendKeys("sourgta#gmail.com");
driver.findElement(By.id("com.offerup:id/next_button")).click();
By path1 = By.xpath("//*[#text='']");
driver.findElement(path1).sendKeys("12manytimes");
driver.findElement(By.id("com.offerup:id/main_text")).click();
}
}
Related
I am trying to do a POC for a windows app using winappdriver . I have the winappdriver version 1.1 installed up and running. I want to find the elements by using their automationId. As per winappdriver documentation, elements with AutiomationID can be located by "findElementByAccessibilityId". I am not able to see this locator strategy in my Eclipse intellisense. Instead "findElementsByAccessibilityId" ( notice elements) is been shown up. What should i do so that i can see "findElementByAccessibilityId" locator in intellisense.
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.windows.WindowsDriver;
public class LoginTest {
private static WindowsDriver<WebElement> driver = null;
#BeforeClass
public static void setup() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("app", "XXXXXXXXXXXXXXXXXXXXXX");
capabilities.setCapability("platformName", "windows");
capabilities.setCapability("deviceName", "windowsPC");
capabilities.setCapability("appWorkingDir", "XXXXXXXXXXXXXXXXXXXXXXXXXX");
driver = new WindowsDriver<WebElement>(new URL("http://127.0.0.1:4723"), capabilities);
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
}
#Test
public void Testing()
{
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.className("TextBox")).sendKeys("XXXX");
driver.findElementById("TxtPwd").sendKeys("XXXX");
driver.findElementsByAccessibilityId("TxtPwd");
driver.findElement(By.id("BtnLogin")).click();
System.out.println("Hi");
}
}
POM.xml
I am using PhantomJs Driver for Headless Testing , I am getting below exception
Sample code:
import static org.testng.Assert.assertEquals;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestLogin {
WebDriver d;
#BeforeMethod
public void launh_Browser() {
System.setProperty("phantomjs.binary.path", "D:\\Selenium\\driver\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
Capabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
d=new PhantomJSDriver(caps);
}
#Test
public void guru_banking_login_excel() throws Exception {
d.get("http://www.demo.guru99.com/V4/");
d.findElement(By.name("uid")).sendKeys("TestUser");
d.findElement(By.name("password")).sendKeys("testpwd");
d.findElement(By.name("btnLogin")).click();
try{
Alert alt = d.switchTo().alert();
String actualBoxMsg = alt.getText(); // get content of the Alter Message
assertEquals(actualBoxMsg,"User or Password is not valid");
alt.accept();
}
catch (NoAlertPresentException Ex){
String hometitle=d.getTitle();
assertEquals(hometitle,"Guru99 Bank Manager HomePage");
}
d.quit
}
Error Observed :
Exception : org.openqa.selenium.UnsupportedCommandException: Invalid Command Method - {"headers":{"Accept-Encoding":"gzip,deflate","Cache-Control":"no-cache","Connection":"Keep-Alive","
I am trying to handle popup using phantomjs as a driver
Please help on This .........
Thanks in Advance..!!!!
You have to take care of a lot of points here in your code :
While working with PhantomJS when you mention System.setProperty use the following code lines instead :
File src = new File("C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path", src.getAbsolutePath());
DesiredCapabilities type of objects must be initiated with reference to DesiredCapabilities class only. So change to :
DesiredCapabilities caps = new DesiredCapabilities();
While using assertEquals use Assert.assertEquals as follows :
Assert.assertEquals(actualBoxMsg,"User or Password is not valid");
//
Assert.assertEquals(hometitle,"Guru99 Bank Manager HomePage");
As you are using TestNG, for Assert, use org.testng.Assert; instead of static org.testng.Assert.assertEquals; as an import :
import org.testng.Assert;
You need to wrap up the line d.quit() within a separate TestNG Annotated function too as follows :
#AfterMethod
public void tearDown() {
d.quit();
}
Here is your own code block which executes successfully :
package headlessBrowserTesting;
import java.io.File;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class PhantomJS_webdriver_binary
{
WebDriver d;
#BeforeMethod
public void launh_Browser() {
File src = new File("C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path", src.getAbsolutePath());
DesiredCapabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
d=new PhantomJSDriver(caps);
}
#Test
public void guru_banking_login_excel() throws Exception {
d.get("http://www.demo.guru99.com/V4/");
d.findElement(By.name("uid")).sendKeys("TestUser");
d.findElement(By.name("password")).sendKeys("testpwd");
d.findElement(By.name("btnLogin")).click();
try{
Alert alt = d.switchTo().alert();
String actualBoxMsg = alt.getText();
Assert.assertEquals(actualBoxMsg,"User or Password is not valid");
alt.accept();
}
catch (NoAlertPresentException Ex){
String hometitle=d.getTitle();
Assert.assertEquals(hometitle,"Guru99 Bank Manager HomePage");
}
}
#AfterMethod
public void tearDown() {
d.quit();
}
}
I am trying this cross-browser testing using Selenium.
CrossBrowser.java:
package automationFramewok;
import java.net.MalformedURLException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.beust.jcommander.Parameters;
// I am getting the following error on the next line
//
// "The attribute value is undefined for the annotation type Parameters"
//
#Parameters({"browser"})
public class CrossBrowser {
#SuppressWarnings("deprecation")
#BeforeTest
public void setUp(String browser) throws MalformedURLException {
if (browser.equalsIgnoreCase("Firefox")) {
System.out.println("Running Firefox");
System.setProperty("webdriver.gecko.driver","E:\\\\Selenium-required files\\geckodriver\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("chrome")) {
System.out.println("Running Chrome");
System.setProperty("webdriver.chrome.driver", "E:\\\\\\\\Selenium-required files\\\\chromedriver\\\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("opera")) {
System.out.println("Running Opera");
// driver = new OperaDriver(); --Use this if the location is set properly--
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("opera.binary", "C://Program Files (x86)//Opera//opera.exe");
capabilities.setCapability("opera.log.level", "CONFIG");
System.setProperty("webdriver.opera.driver", "E:\\\\\\\\Selenium-required files\\\\operadriver\\\\operadriver.exe");
OperaDriver driver = new OperaDriver(capabilities);
}
}
}
I am receiving the following error message:
The attribute value is undefined for the annotation type Parameters
How can I resolve this?
Check out your list of import statements. I think you want
import org.testng.annotations.Parameters;
and not
import com.beust.jcommander.Parameters;
The same issue I was facing and problem was with import statement. I was using the following import statement.
import org.junit.runners.Parameterized.Parameters;
Replaced with the following import statement and issue got resolved.
import org.testng.annotations.Parameters;
i am automating Odoo pos application. but while adding product to the cart, i got error like "Element is not clickable at point (659,166)". i have already created 1 item and trying to add that in cart but element is not found. need help.
package odoo1;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class odoo {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
// for Log in functionality
driver.get("https://www.odoo.com/trial?selected_app=point_of_sale");
driver.findElement(By.id("username")).sendKeys("mark");
driver.findElement(By.id("email")).sendKeys("mark#gmail.com");
driver.findElement(By.id("company-name")).sendKeys("odooo");
driver.findElement(By.id("phone")).sendKeys("561234897");
driver.findElement(By.id("country-id")).sendKeys("India");
driver.findElement(By.name("lang")).sendKeys("English");
Select sell=new Select(driver.findElement(By.name("company_size")));
sell.selectByIndex(2);;
Select sell1=new Select(driver.findElement(By.id("plan")));
sell1.selectByIndex(2);
driver.findElement(By.xpath(".//*[#id='wrapwrap']/main/div/div/div[2]/div[2]/div/form/input[8]")).click();
// for creating product
driver.findElement(By.xpath("html/body/div[3]/div[3]/div[1]/a[2]/div[2]")).click();
driver.findElement(By.xpath("html/body/nav/div/ul[1]/li[2]/a")).click();
driver.findElement(By.xpath("html/body/nav/div/ul[1]/li[2]/ul/li[3]/a/span")).click();
driver.findElement(By.xpath("html/body/div[3]/div[2]/div[1]/div/button")).click();
driver.findElement(By.xpath(".//*[#id='o_field_input_4']")).sendKeys("iphone");
driver.findElement(By.xpath(".//*[#id='o_field_input_4']")).click();
driver.findElement(By.xpath(".//*[#id='o_field_input_13']")).sendKeys("abcd");
driver.findElement(By.xpath(".//*[#id='o_field_input_14']")).sendKeys("123456");
driver.findElement(By.xpath(".//*[#id='o_field_input_15']"));
driver.findElement(By.xpath(".//*[#id='o_field_input_16']")).clear();
driver.findElement(By.xpath(".//*[#id='o_field_input_16']")).sendKeys("25000");
driver.findElement(By.xpath(".//*[#id='o_field_input_20']")).clear();
driver.findElement(By.xpath(".//*[#id='o_field_input_20']")).sendKeys("25000");
Thread.sleep(1000);
driver.findElement(By.xpath("html/body/div[3]/div[2]/div[1]/div/div[2]/button[1]")).click();
Thread.sleep(900);
driver.findElement(By.xpath("html/body/nav/div/ul[1]/li[1]/a/span")).click();
driver.findElement(By.xpath("html/body/div[4]/div/div/div[1]/div[2]/div/div[1]/button")).click();
Thread.sleep(500);
driver.findElement(By.xpath("html/body/div[1]/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[2]/table/tbody/tr[2]/td/div/div/div/div/span[2]/div[1]")).click();
Thread.sleep(700);
driver.findElement(By.xpath("//button")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("html/body/div[1]/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[2]/table/tbody/tr[2]/td/div/div/div/div/span[2]/div[1]/img")).click();
}
}
By looking at the exception, it looks like the right path is not selected.
I have just checked the website which you are trying to automate.
Steps I have followed:
Click on Open Cart tab.
Select Shipping option.
Click on Add to Cart for the product which you want to add in your path.
Xpath Used for Add To Cart button:
//h3[normalize-space()="Opencart Marketplace"]/../div[3]//button
I have selected the product "Opencart Marketplace". You can select any product you want. Just replace Opencart Marketplace with the product which you want to select.
Code:
System.setProperty("webdriver.gecko.driver", "src/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://store.webkul.com/OpenCart-Modules.html");
Thread.sleep(2);
driver.findElement(By.xpath("//h3[normalize-space()='Opencart Marketplace']/../div[3]//button")).click();
driver.findElement(By.xpath("//div[#class='product_cart_container']/button")).click();
I'm trying to run a test in eclipse using appium for android. While the test starts and the application launches it throws an error on the first command while trying to press a button. I'm pretty sure that the id for the button is correct since running the same code with selendroid worked. The error at failure trace shows java.lang.NullPointerException. Here is my code:
package thePack;
import static org.junit.Assert.*;
import io.appium.java_client.AppiumDriver;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
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.interactions.Actions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class theTest {
static AppiumDriver driver;
#Before
public void setUp() throws MalformedURLException, InterruptedException, Exception
{
WebDriver dr;
File app = new File("C:\\development\\src\\main\\resources\\app.apk");
DesiredCapabilities capabilities= new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName", "Vodafone Smart 4G");
capabilities.setCapability("platformVersion", "4.2.2");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appium-version", "1.2.1");
capabilities.setCapability("appPackage", "mypackage.mine.net");
capabilities.setCapability("appActivity", "mypackage.mine.net.activities.mainActivity");
dr = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities) ;
}
#Test
public void login() throws Exception
{
Thread.sleep(0500);
driver.findElement(By.id("english")).click();
Thread.sleep(0500);
}
Any ideas? Thanks!
the line:
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
indicates that your test will be performed on the browser, and this should open the browser when start your test. Thus your elements will never be found
Figured out the issue. For me the problem was the Appium version itself. I was running the 1.3.4.1 and using an Android 4.2.2 device. As soon as i switched to version 1.2.4.1 for Appium i had no problems!