Unsupported CSS selector '*[name='username']'. Reason: 'Error: 'name' is not a valid attribute - Appium PageFactory - appium

Hi all!
I've been having the next issue:
I'm trying to use PageFactory in my appium project, but haven't been able.
public class LoginPageObjects {
AppiumDriver driver;
#AndroidFindBy(id = "username")
public WebElement username;
#AndroidFindBy(id = "password")
public WebElement passwordElement;
#AndroidFindBy(xpath = "//android.view.ViewGroup[#content-desc=\"loginBtn\"]/android.widget.TextView\n")
public WebElement buttonElement;
public LoginPageObjects(AppiumDriver driver){
this.driver = driver;
PageFactory.initElements(driver,this);
}
public void setUsernameTextBox(String text){
username.sendKeys(text);
}
public void setPasswordTextBox(String text){
passwordElement.sendKeys(text);
}
public void clickLoginButton() {
passwordElement.click();
}
}
It's not working this way, having the following error calling username.sendKeys():
Unsupported CSS selector '*[name='username']'. Reason: 'Error: 'name' is not a valid attribute.
where I have also tried
PageFactory.initElements(new AppiumFieldDecorator(driver),this);
But this throws a new error
Currently using:
io.appium 8.0.0;
org.seleniumhq.selenium 4.1.3;
JDK v18
Pst: PageFactory in Selenium with the same versions is working fine.

Related

IllegalArgumentException while initialising locators

i am trying to automate IOS App and while i initialising locators getting below error,
java.lang.IllegalArgumentException: Can not set static io.appium.java_client.MobileElement field Modules.Order.allMenus to jdk.proxy2.$Proxy11
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at java.base/jdk.internal.reflect.UnsafeStaticObjectFieldAccessorImpl.set(UnsafeStaticObjectFieldAccessorImpl.java:79)
at java.base/java.lang.reflect.Field.set(Field.java:799)
at org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:117)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:105)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:92)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:79)
Below is the script,
import java.io.IOException;
public class Order {
private static AppiumDriver driver;
public Order(AppiumDriver driver){
this.driver = driver;
PageFactory.initElements(driver,this);
}
#iOSXCUITFindBy(iOSClassChain = "**/XCUIElementTypeButton[`label == 'All menus, tab, 4 of 4'`]")
public static MobileElement allMenus;
public static MobileElement getAllMenus(){
return allMenus;
}
public static void placeOrder() throws IOException {
CommonUtils.waitForElement(getAllMenus());
}
}
the error line is pointing to
PageFactory.initElements(driver,this);

Null pointer exception in pagefactory declaration for ios driver used when i use #iOSXCUITFindBy for java client 7.3.0 with selenium 3.141.59

What should be the page factory declaration when i use #iOSXCUITFindBy
I get null pointer exception
I use java client 7.3.0 and selenium 3.141.59
My current code is
public class IOSPageFactory {
public IOSPageFactory(IOSDriver<IOSElement> driver) {
regBase.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver), IOSPageFactory.class);
}
#iOSXCUITFindBy (xpath = "//XCUIElementTypeButton[#name='SIGN IN']")
public static WebElement YellowSignIn1;
final String URL_STRING = "http://0.0.0.0:4723/wd/hub";
regBase.url = new URL(URL_STRING);
regBase.capabilities = new DesiredCapabilities();
regBase.capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "***");
regBase.capabilities.setCapability(MobileCapabilityType.UDID, "*******");
regBase.capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
regBase.capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "12.4");
regBase.capabilities.setCapability(MobileCapabilityType.APP, "/Users/*******");
regBase.capabilities.setCapability(MobileCapabilityType.FULL_RESET, true);
regBase.capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");
regBase.capabilities.setCapability("useNewWDA", true);
regBase.capabilities.setCapability("xcodeOrgId", "*******");
regBase.capabilities.setCapability("xcodeSigningId", "iPhone Developer");
regBase.capabilities.setCapability("agentPath", "/usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent/WebDriverAgent.xcodeproj");
regBase.capabilities.setCapability("bootstrapPath", "/usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent");
regBase.capabilities.setCapability("updatedWDABundleId", "*********");
regBase.capabilities.setCapability(IOSMobileCapabilityType.AUTO_ACCEPT_ALERTS, false); // True will auto accept
regBase.iosDriver = new IOSDriver<IOSElement>(regBase.url, regBase.capabilities);
regBase.iosDriver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
Thread.sleep(10000);
//Initializing ios page factory :
import io.appium.java_client.ios.IOSDriver; import io.appium.java_client.ios.IOSElement;
public class IOSPageFactory {
public IOSPageFactory(IOSDriver<IOSElement> driver) {
this.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}
#iOSXCUITFindBy (xpath = "//XCUIElementTypeButton[#name='SIGN IN']")
public static WebElement YellowSignIn1;
Driver type has to be an AppiumDriver.
public IOSPageFactory(AppiumDriver<MobileElement> driver) {
this.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}

Appium - PageFactory initElements is not working

I have this Class as my page object:
public class LaunchPageObject {
private AppiumDriver<AndroidElement> driver;
public LaunchPageObject() {
}
public LaunchPageObject(AppiumDriver<AndroidElement> driver) {
this.driver=driver;
PageFactory.initElements(new AppiumFieldDecorator(this.driver), this);
}
public void Click_SigninNow() {
lnk_SigninNow.click();
}
#AndroidFindBy(xpath="//android.widget.Button[#text='LOGIN WITH FACEBOOK']")
MobileElement btn_SignupWithEmail;
#AndroidFindBy(xpath="//android.widget.Button[#text='SIGN UP WITH EMAIL']")
MobileElement btn_LoginWithFacebook;
#AndroidFindBy(xpath="//android.widget.TextView[#text='Sign in now']")
MobileElement lnk_SigninNow;
}
and I have this class as my test case class:
public class LaunchPageTest extends Android {
#Test
public void Click_SigninNow() throws MalformedURLException {
LaunchPageObject lp = new LaunchPageObject(setDriver());
lp.Click_SigninNow();
}
}
I have this error log:
FAILED: Click_SigninNow
java.lang.ExceptionInInitializerError
at io.appium.java_client.pagefactory.utils.ProxyFactory.getEnhancedProxy(ProxyFactory.java:52)
at io.appium.java_client.pagefactory.utils.ProxyFactory.getEnhancedProxy(ProxyFactory.java:33)
at io.appium.java_client.pagefactory.AppiumFieldDecorator.proxyForAnElement(AppiumFieldDecorator.java:217)
at io.appium.java_client.pagefactory.AppiumFieldDecorator.access$0(AppiumFieldDecorator.java:215)
at io.appium.java_client.pagefactory.AppiumFieldDecorator$1.proxyForLocator(AppiumFieldDecorator.java:107)
at org.openqa.selenium.support.pagefactory.DefaultFieldDecorator.decorate(DefaultFieldDecorator.java:62)
at io.appium.java_client.pagefactory.AppiumFieldDecorator.decorate(AppiumFieldDecorator.java:155)
at org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:113)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:105)
at POM.LaunchPageObject.(LaunchPageObject.java:35)
at TestCases.LaunchPageTest.Click_SigninNow(LaunchPageTest.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
The test opens the app but is not able to click the element. Any idea what is happening here?
Keep your page object and test classes like below
public class LaunchPageObject {
#AndroidFindBy(xpath="//android.widget.Button[#text='LOGIN WITH FACEBOOK']")
MobileElement btn_SignupWithEmail;
#AndroidFindBy(xpath="//android.widget.Button[#text='SIGN UP WITH EMAIL']")
MobileElement btn_LoginWithFacebook;
#AndroidFindBy(xpath="//android.widget.TextView[#text='Sign in now']")
MobileElement lnk_SigninNow;
public void click_SigninNow() {
lnk_SigninNow.click();
}
}
public class LaunchPageTest extends Android {
LaunchPageObject lp = PageFactory.initElements(getDriver(), LaunchPageObject.class);
#Test
public void Click_SigninNow() throws MalformedURLException {
lp.Click_SigninNow();
}
}

Jedis Cache implementation without JedisPool/commons-pool2-2.0

How to implement Jedis without JedisPool/commons-pool2-2.0 because still we are using jdk 1.5(commons-pool2-2.0 does not support JDK 1.5)
How to implement a thread-safe connection pooling?
I'm not sure about Jedis compatibility with Java 5. You can create your own pooling based on the older commons-pool 1.6 library. You do not need to have commons-pool2 on your class path to run jedis. I used Jedis 2.7.3 and commons-pool 1.6 to validate the solution approach.
Find the example code attached:
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import redis.clients.jedis.Jedis;
public class JedisWithOwnPooling {
public static void main(String[] args) throws Exception {
ObjectPool<Jedis> pool = new GenericObjectPool(new JedisFactory("localhost"));
Jedis j = pool.borrowObject();
System.out.println(j.ping());
pool.returnObject(j);
pool.close();
}
private static class JedisFactory implements PoolableObjectFactory<Jedis> {
private String host;
/**
* Add fields as you need. That's only an example.
*/
public JedisFactory(String host) {
this.host = host;
}
#Override
public Jedis makeObject() throws Exception {
return new Jedis(host);
}
#Override
public void destroyObject(Jedis jedis) throws Exception {
jedis.close();
}
#Override
public boolean validateObject(Jedis jedis) {
return jedis.isConnected();
}
#Override
public void activateObject(Jedis jedis) throws Exception {
if (!jedis.isConnected()) {
jedis.connect();
}
}
#Override
public void passivateObject(Jedis jedis) throws Exception {
}
}
}

MonoDroid & RestSharp, MethodMissingException

I'm trying to use RestSharp with MonoDroid and I'm running into some problems. A MethodMissingException is thrown with the message "Default constructor not found for type EmPubLite.Android.Tweet[]." I can only see the exception when I inspect the Task in the VS debugger, it doesn't show up in LogCat. I know that the exception is related to the JSON deserialization in RestSharp.
The linker is set to 'None' in MonoDroid options, and I also tried adding the [Preserve] attribute to my DTOs.
Beyond getting this to work, why doesn't this crash and show up in LogCat? I'd like any Tasks that fail with exceptions to rethrow the exception when I access the result.
[Activity(Label = "EmPubLite", MainLauncher = true, Icon = "#drawable/icon")]
public class EmPubLiteActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.EmPubLight);
var client = new TwitterClient();
client.Search("bielema").ContinueWith(tweets =>
{
Debug.WriteLine("back");
Debug.WriteLine(tweets.Result[0].Text); // Exception not showing up in LogCat
});
}
}
public class TwitterClient
{
private RestClient client = new RestClient();
public Task<Tweet[]> Search(string query)
{
var request = new RestRequest("http://search.twitter.com/search.json?rpp=5&include_entities=true&result_type=mixed&q={q}", Method.GET);
request.AddUrlSegment("q", query);
var taskCompletionSource = new TaskCompletionSource<Tweet[]>();
client.ExecuteAsync<SearchResult>(request, response =>
{
if (response.ErrorException != null) taskCompletionSource.TrySetException(response.ErrorException);
else taskCompletionSource.TrySetResult(response.Data.Results);
// taskCompletionSource.TrySetResult(response.Data.Results);
});
return taskCompletionSource.Task;
}
}
public class SearchResult
{
public Tweet[] Results { get; set; }
}
public class Tweet
{
public string Text { get; set; }
}
I fixed this by using the ServiceStack JSON parser instead of the default RestSharp JSON parser.

Resources