0

I want to automate mobile web site testing on Android emulator using c# and Appium. There is a simple test scenario I want to automate for the start:
1. Start Browser
2. Find an element
3. Clear it
4. Send keys

I've got a problem with the second step. Every time MSTest tries to execute FindElementById line in the code below, I get the error: "An element could not be located on the page using the given search parameters."

[TestClass]
public class UnitTest1
{
    private DesiredCapabilities _capabilities;
    private AndroidDriver _driver;

    public void InitializeDriver()
    {
        Console.WriteLine("Connecting to Appium server");
        _capabilities = new DesiredCapabilities();

        _capabilities.SetCapability("deviceName", "test_02");
        _capabilities.SetCapability(CapabilityType.BrowserName, "Chrome");
        _capabilities.SetCapability(CapabilityType.Version, "5.0.1");
        _capabilities.SetCapability(CapabilityType.Platform, "Android");

        //Application path and configurations
        _driver = new AndroidDriver(new Uri("http://127.0.0.1:4723/wd/hub"), _capabilities);
    }

    [TestMethod]
    public void TestMethod1()
    {
        InitializeDriver();

        var element = _driver.FindElementById("com.android.browser:id/url");
        element.Clear();
        element.SendKeys(@"http://stackoverflow.com/");
    }
}

Input string for the method I've got from UIAutomator that is shown below.

1

I tried several combinations for the FindElementById input method:
"com.android.browser:id/url"
"id/url"
"url"
but no luck.

My environment:
Windows 8.1
Appium 1.3.4.1
ChromeDriver 2.14.313457
Android Device Monitor 24.0.2

Paul
  • 78
  • 1
  • 3
  • 12

3 Answers3

0

Update ! The following approach is not for web testing:

Could you try to find the element using xpath?

@FindBy(xpath="//android.widget.EditText[contains(@resource-id, 'url')]")

So in your case you can try the following:

var element = _driver.findElementByXPath("//android.widget.EditText[contains(@resource-id, 'url')]");

Update: in case of testing web apps (not native) you should use web page locators instead of Android classes.

Alex
  • 362
  • 1
  • 5
  • 14
  • Thank you for a quick answer, but it doesn't fix the problem. What language do you use? Is it Java? Is my code correct, i mean is it the same way you do in your language? I think maybe there is something wrong with intermediate libraries, such as Appium or ChromeDriver. I think it would be better to add those info. – Paul Mar 02 '15 at 11:41
  • I use Java, but had the same issue with using `findElementById` approach, so for now I use only `findElementByXPath`. Also try to use some timeout or "wait for" approach – Alex Mar 02 '15 at 11:48
  • Can you give me an example of "wait for" approach, please? Is it a setting in Appium or just block of code? – Paul Mar 02 '15 at 11:59
  • The examples are listed here in the answers: https://stackoverflow.com/questions/28799420/how-to-wait-to-activity-using-appium-on-begin-and-during-test-itself They are in Java, but I think they will be helpfull anyway. – Alex Mar 03 '15 at 07:38
  • Thanks a lot, Jean-Pierre! Those links are very helpful! – Paul Mar 03 '15 at 11:20
0

Try these 2 statements:

var element = _driver.FindElement(By.Id("com.android.browser:id/url");
driver.findElementsByXPath("//*[@class='com.android.browser' and @index='1']");
Gaurav
  • 1,332
  • 11
  • 22
0

Sorry for misleading !!! In case of testing web apps in browser the elements should be located as usual elements on the web page ( not as some classes like android.widget.EditText and android.widget.Button). So try for example the following and you will see some result:

    var element = _driver
            .findElementByXPath("//input[@id='lst-ib']");

To get locators you should run the browser on your desktop, open the page and use some tools/extensions like Firebug in Firefox or Firebug Lite in Chrome browser.

Alex
  • 362
  • 1
  • 5
  • 14
  • Thanks, it worked! What do you use to find locators id? Is it Chrome Developer Tools? – Paul Mar 02 '15 at 17:01
  • Can you recommend any tutorials on how to automate web apps? – Paul Mar 02 '15 at 17:05
  • in Chrome I use the following extensions: **Firebug Lite** to find locators and **xPath Viewer** to test my xpath expressions. But if you use Firefox, I would recommend to use Firebug and also the following:FirePath, Firediff, Firefinder – Alex Mar 02 '15 at 17:13
  • You can learn Selenium in this site: http://www.tutorialspoint.com/selenium/ and there some good Selenium and Appium courses on Udemy.com (both free and paid courses) – Alex Mar 02 '15 at 17:17
  • To learm xpath I would recommend http://www.zvon.org/xxl/XPathTutorial/General/examples.html and http://www.w3schools.com/xpath/default.asp – Alex Mar 02 '15 at 17:25