Finding elements for iOS web-based apps using Safari's Develop option

Safari comes with a built-in solution for finding the elements for web apps, but we need to perform the following steps in order to set up the device for remote debugging:

  1. Navigate to Settings | Safari | Advanced:
    Finding elements for iOS web-based apps using Safari's Develop option
  2. Then, turn on Web Inspector:
    Finding elements for iOS web-based apps using Safari's Develop option
  3. Open the Safari browser on your device/simulator and navigate to the desired URL (we are navigating to www.google.com).
  4. Now, go to your Mac Safari browser, click on the Develop option from the menu, select the device/simulator (assuming that the device is connected to your Mac), and click on the URL:
    Finding elements for iOS web-based apps using Safari's Develop option
  5. Now you will get the following screen with the HTML source code:
    Finding elements for iOS web-based apps using Safari's Develop option

Now, we can easily inspect elements with Safari DevTools.

Finding elements by ID

To interact with the web page, first we need to find an element on the page. All the functions of the Appium client library need an element to perform the actions on the web page.

Finding an element by ID is used to locate only one element in the mobile app. This is how the method signature to find an element looks:

findElement(By.id(String id));

We need to pass an ID of the element we want to interact with. Now, we are going to find the ID of an element using the Chrome ADB plugin remotely. Here, we have taken an example of the Google search page. Perform the following steps:

  1. Navigate to https://www.google.com on your mobile's Chrome browser.
  2. Click on the inspect link from the ADB plugin of your desktop's Chrome browser.
  3. Click on the inspect element icon Finding elements by ID and mouseover the search box, as shown in the following screenshot:
    Finding elements by ID
    We can use the highlighted id to interact with the web element. This is how the command will look:
    WebElement searchBox=driver.findElement(By.id("lst-ib"));

If you want to type in the search box, then you can use a web element reference; for instance, you can use a reference such as searchBox.sendKeys("Manoj Hans");.

Let's take the same example to find an element by ID on the Safari browser in an iOS device. We need to perform the following steps:

  1. Navigate to https://www.google.com on your mobile Safari browser.
  2. Click on the URL under iOS simulator under the Develop tab of the Mac Safari browser.
  3. Click on the Inspect icon and then click on the search box in the iOS simulator, as shown here:
    Finding elements by ID

We can use the highlighted id to interact with the web element. This is how the command will look:

WebElement searchBox=driver.findElement(By.id("lst-ib"));

If you want to type in the search box, then you can use a web element reference, such as searchBox.sendKeys("Manoj Hans");.

Finding elements by name

Another way to find an element is by their name; elements can have names to locate them. This is how the method signature will look:

findElement(By.name(String Name));

Same as in the case of id, we need to pass the name attribute of the element we want to look for. It will return a WebElement object that we can perform actions on. We can again take an example of the Google search page, as the search box also has a name. All the steps will be the same as the ones we've taken to find an element by ID, as shown in the following screenshot:

Finding elements by name

This is how the command will look:

WebElement searchBox=driver.findElement(By.name("q"));

Finding elements by linkText

This method is useful when you want to locate the element that has a hyperlink. This is how the method signature looks:

findElement(By.linkText(String text));

We need to pass the text that has a hyperlink. It will return a WebElement object that we can perform actions on. All the steps to locate the element will be the same as the steps performed to find an element by ID.

We are going to find the Images text on the Google search page that have a link; this is how the command will look:

WebElement imagesLink=driver.findElement(By.linkText("Images"));

Finding elements by Xpath

Xpath works on both XML and well-formed HTML structures to find elements. It is a bit slower (in the cases where you generated it in a complex manner) than the ID and name methods, but it is a very useful approach to find an element on the web page where the element ID is generated dynamically. Here, we are not going to teach you about Xpath, but if you want to learn about the Xpath strategy, then you can search Google for a tutorial. This is how the method signature will look:

findElement(By.xpath(String XPath));

We need to pass the Xpath of the element we want to look for. It will return a WebElement object that we can perform actions on. Here, we will construct the Xpath on the basis of attributes.

We are going to construct an Xpath of the Google search box; this is how the command will look:

WebElement searchBox=driver.findElement(By.xpath("//input[@id='lst-ib']"));

Finding elements by cssSelector

cssSelector strictly operates on HTML, and it is faster than Xpath in finding an element on the web page. This is how the method signature will look:

findElement(By.cssSelector(String cssSelector);

We need to pass the cssSelector of the element we want to look for. It will return a WebElement object that we can perform actions on. Here, we will construct the cssSelector on the basis of attributes.

We are going to construct cssSelector of the Google search box; this is how the command will look:

WebElement searchBox=driver.findElement(By.cssSelector("#lst-ib"));
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.96.102