A guide for ignoring dynamic elements in Appium or WebdriverIO test scripts.
You can have elements on your screen such as dates or headlines that change dynamically. Such elements that change frequently, can pose challenges when it comes to consistent visual analysis. To avoid irrelevant differences in App Percy, you can either mock these elements to maintain consistent values or ignore these regions entirely within your App Percy test script.
To ignore specific regions within your script, App Percy offers the below options to ignore elements:
With App Percy, you can modify your script to ignore specific elements using the following options:
XPath
Accessibility ID
Pixel-based coordinates
By directly identifying the elements in your test script
The sample code for each option is shown below:
Sample code
Ignore elements using XPath
```java
import io.percy.appium.AppPercy;
import io.percy.appium.lib.IgnoreRegion;
public class Example {
private static AppPercy percy;
public static void main(String[] args) throws MalformedURLException, InterruptedException {
DesiredCapabilities caps = new DesiredCapabilities();
// Add caps here
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
new URL("http://hub.browserstack.com/wd/hub"), caps);
percy = new AppPercy(driver);
// Add ignore region options here
ScreenshotOptions options = new ScreenshotOptions();
List<String> ignoreRegionXpaths = new ArrayList<String>();
// add valid xpaths of element that you want to ignore in visual diff
ingoreRegionXpaths.add("some_valid_xpath");
// add those to options
options.setIgnoreRegionXpaths(ignoreRegionXpaths);
// pass options in screenshot call
percy.screenshot("Screeshot name", options);
driver.quit();
}
}
```
```csharp
using PercyIO.Appium.
namespace csharp_appium__jwp_first_android_test_browserstack
{
class Android
{
static void Main(string[] args)
{
AppiumOptions capabilities = new AppiumOptions();
// Add caps here
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
new Uri("https://hub-cloud.browserstack.com/wd/hub"), capabilities);
// Initialize AppPercy
AppPercy appPercy = new AppPercy(driver);
var options = new ScreenshotOptions();
// Add ignore region options here
List<String> xpaths = new List<string>();
// add valid xpath of element that you want to ignore
xpaths.Add("some_valid_xpaths");
// add those to options
options.IgnoreRegionXpaths = xpaths;
// pass options in screenshot call
appPercy.Screenshot("Screeshot name", options);
driver.Quit();
}
}
}
```
```python
from appium import webdriver
from percy import percy_screenshot
from percy.lib.ignore_region import IgnoreRegion
driver = webdriver.Remote("http://hub.browserstack.com/wd/hub", options)
// add valid xpath of element that you want to ignore
ignore_region_xpaths = ["some_valid_xpath"]
// pass options in screenshot call
percy_screenshot(driver, 'Screeshot name'
ignore_regions_xpaths = ignore_region_xpaths,
)
```
```java
import io.percy.appium.AppPercy;
import io.percy.appium.lib.IgnoreRegion;
public class Example {
private static AppPercy percy;
public static void main(String[] args) throws MalformedURLException, InterruptedException {
DesiredCapabilities caps = new DesiredCapabilities();
// Add caps here
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
new URL("http://hub.browserstack.com/wd/hub"), caps);
percy = new AppPercy(driver);
// Add ignore region options here
ScreenshotOptions options = new ScreenshotOptions();
List<String> ignoreRegionAccessibilityIds = new ArrayList<String>();
// add valid accessibility id of elements that you want to ignore
ignoreRegionAccessibilityIds.add("some_valid_id");
// add those to options
options.setIgnoreRegionAccessibilityIds(ignoreRegionAccessibilityIds);
// pass options in screenshot call
percy.screenshot("Screeshot name", options);
driver.quit();
}
}
```
```csharp
using PercyIO.Appium.
namespace csharp_appium__jwp_first_android_test_browserstack
{
class Android
{
static void Main(string[] args)
{
AppiumOptions capabilities = new AppiumOptions();
// Add caps here
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
new Uri("https://hub-cloud.browserstack.com/wd/hub"), capabilities);
// Initialize AppPercy
AppPercy appPercy = new AppPercy(driver);
var options = new ScreenshotOptions();
// Add ignore region options here
List<String> accessiblityIds = new List<string>();
// add valid accessibility id of element that you want to ignore
accessiblityIds.Add("Login");
// add those to options
options.IgnoreRegionAccessibilityIds = accessiblityIds;
// pass options in screenshot call
appPercy.Screenshot("Screeshot name", options);
driver.Quit();
}
}
}
```
```python
from appium import webdriver
from percy import percy_screenshot
from percy.lib.ignore_region import IgnoreRegion
driver = webdriver.Remote("http://hub.browserstack.com/wd/hub", options)
// add valid accessibility id of element that you want to ignore
ignore_regions_ids = ["some_valid_id"]
// pass options in screenshot call
percy_screenshot(driver, 'Screeshot name'
ignore_region_accessibility_ids= ignore_region_ids,
)
```
```java
import io.percy.appium.AppPercy;
import io.percy.appium.lib.IgnoreRegion;
public class Example {
private static AppPercy percy;
public static void main(String[] args) throws MalformedURLException, InterruptedException {
DesiredCapabilities caps = new DesiredCapabilities();
// Add caps here
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
new URL("http://hub.browserstack.com/wd/hub"), caps);
percy = new AppPercy(driver);
// Add ignore region options here
ScreenshotOptions options = new ScreenshotOptions();
List<IgnoreRegion> customIgnoreRegions = new ArrayList<IgnoreRegion>();
// add valid custom loction of region that you want to ignore in px
IgnoreRegion ignoreRegion = new IgnoreRegion(10, 20, 100, 200); # top, bottom, left, right
customIgnoreRegions.add(ignoreRegion);
// add those to options
options.setCustomIgnoreRegions(customIgnoreRegions);
// pass options in screenshot call
percy.screenshot("Screeshot name", options);
driver.quit();
}
}
```
```csharp
using PercyIO.Appium.
namespace csharp_appium__jwp_first_android_test_browserstack
{
class Android
{
static void Main(string[] args)
{
AppiumOptions capabilities = new AppiumOptions();
// Add caps here
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
new Uri("https://hub-cloud.browserstack.com/wd/hub"), capabilities);
// Initialize AppPercy
AppPercy appPercy = new AppPercy(driver);
var options = new ScreenshotOptions();
// Add ignore region options here
List<IgnoreRegion> customIgnoreRegions = new List<AppiumWebElement>();
// add valid custom loction of region that you want to ignore in px
var ignoreRegion = new IgnoreRegion(10, 200, 300, 500); // top, bottom, left, right
customIgnoreRegions.Add(ignoreRegion);
// add those to options
options.CustomIgnoreRegions = customIgnoreRegions;
// pass options in screenshot call
appPercy.Screenshot("Screeshot name", options);
driver.Quit();
}
}
}
```
```python
from appium import webdriver
from percy import percy_screenshot
from percy.lib.ignore_region import IgnoreRegion
driver = webdriver.Remote("http://hub.browserstack.com/wd/hub", options)
// add valid custom loction of region that you want to ignore in px
custom_ignore_regions = [IgnoreRegion(top=1199, bottom=1225,left=108, right=464)]
// pass options in screenshot call
percy_screenshot(driver, 'Screeshot name'
custom_ignore_regions= custom_ignore_regions
)
```
```java
import io.percy.appium.AppPercy;
import io.percy.appium.lib.IgnoreRegion;
public class Example {
private static AppPercy percy;
public static void main(String[] args) throws MalformedURLException, InterruptedException {
DesiredCapabilities caps = new DesiredCapabilities();
// Add caps here
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
new URL("http://hub.browserstack.com/wd/hub"), caps);
percy = new AppPercy(driver);
// Add ignore region options here
ScreenshotOptions options = new ScreenshotOptions();
List<MobileElement> ignoreRegionAppiumElements = new ArrayList<MobileElement>();
// add valid element that you want to ignore
ignoreRegionAppiumElements.add(some_valid_element);
// add those to options
options.setIgnoreRegionAppiumElements(ignoreRegionAppiumElements);
// pass options in screenshot call
percy.screenshot("Screeshot name", options);
driver.quit();
}
}
```
```csharp
using PercyIO.Appium.
namespace csharp_appium__jwp_first_android_test_browserstack
{
class Android
{
static void Main(string[] args)
{
AppiumOptions capabilities = new AppiumOptions();
// Add caps here
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
new Uri("https://hub-cloud.browserstack.com/wd/hub"), capabilities);
// Initialize AppPercy
AppPercy appPercy = new AppPercy(driver);
var options = new ScreenshotOptions();
// Add ignore region options here
List<AppiumWebElement> appiumWebElements = new List<AppiumWebElement>();
// add valid element that you want to ignore
appiumWebElements.Add(some_valid_element);
// add those to options
options.IgnoreRegionAppiumElements = appiumWebElements;
// pass options in screenshot call
appPercy.Screenshot("Screeshot name", options);
driver.Quit();
}
}
}
```
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback
ON THIS PAGE
Is this page helping you?
Yes
No
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback!
We use cookies to enhance user experience, analyze site usage, and assist in our marketing efforts. By continuing to browse
or closing this banner, you acknowledge that you have read and agree to our Cookie Policy,
Privacy Policy and Terms of Service.