How to use driver.element method in Appium

Best JavaScript code snippet using appium

Run Appium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

follows.test.js

Source: follows.test.js Github

copy
1import it from '../helpers/appium';
2import server from '../helpers/server';
3import fixtures from '../helpers/fixtures';
4import bootstrap from '../helpers/bootstrap';
5
6describe("Follows", () => {
7  it("should show my follows", function* (driver, done) {
8    server.get("/api/follows/tester", fixtures.myFollows());
9
10    yield bootstrap().login().nav("dashboard/follows").launch(driver);
11    
12    yield driver.elementById('Dashboard');
13    yield driver.elementById('friend');
14    yield driver.elementById('follow2');
15
16    done();
17  });
18
19  it("should show an empty list", function* (driver, done) {
20    server.get("/api/follows/tester", { username: "tester", follows: [] });
21
22    yield bootstrap().login().nav("dashboard/follows").launch(driver);
23
24    yield driver.elementById('Follows');
25    yield driver.elementById('No items');
26
27    done();
28  });
29
30  it("should my friends's follows", function* (driver, done) {
31    server.get("/api/follows/friend", fixtures.friendFollows());
32
33    yield bootstrap().login().nav("dashboard/follows/friend/follows").launch(driver);
34
35    yield driver.elementById('friend');
36    yield driver.elementById('tester');
37    yield driver.elementById('follow4');
38
39    done();
40  });
41
42  it("should navigate follows recursively", function* (driver, done) {
43    server.get("/api/posts/tester", fixtures.home());
44    server.get("/api/posts/friend", fixtures.friend());
45    server.get("/api/posts/follow4", { 
46        username: "follow4", 
47        posts: [ { id: 1000, username: 'follow4', content: 'post1000' }]
48    });
49
50    server.get("/api/follows/friend", fixtures.friendFollows());
51    server.get("/api/follows/tester", fixtures.myFollows());
52    server.get("/api/follows/follow4", { username: "follow4", follows: [ { id: 123, username: 'tester' }] });
53
54    yield bootstrap().login().launch(driver);
55
56    yield driver.elementById("Dashboard");
57    yield driver.elementById("post1");  // my post
58    // yield driver.sleep(90000000);
59    yield driver.elementById("segFollows_tester").click(); // open my follows
60
61    yield driver.elementById('friend').click();  // click on friend
62
63    yield driver.elementById("friend"); // friend's list
64    yield driver.elementById("post4");  // friend's post
65    yield driver.elementById("segFollows_friend").click(); // open friend's follows
66
67    yield driver.elementById('follow4').click();  // click on other
68
69    yield driver.elementById("follow4"); // other's list
70    yield driver.elementById("post100"); // other's post
71    yield driver.elementById("segFollows_follow4").click(); // open other's follows
72
73    yield driver.elementById('tester').click(); // click on me
74
75    // TODO: it should NOT say "Dashboard"    
76    yield driver.elementById("tester"); // my list
77    yield driver.elementById("post1");  // my post
78
79    // Now pop everything
80    yield driver.elementById("back").click(); // back
81    yield driver.elementById("follow4");
82
83    yield driver.elementById("back").click(); // back
84    yield driver.elementById("friend");
85
86    yield driver.elementById("back").click(); // back
87
88    yield driver.elementById("Dashboard");
89
90    done();
91  });
92});
93
Full Screen

basics-base.js

Source: basics-base.js Github

copy
1"use strict";
2
3var setup = require("../setup-base"),
4    webviewHelper = require("../../../helpers/webview"),
5    loadWebView = webviewHelper.loadWebView,
6    isChrome = webviewHelper.isChrome,
7    Q = require('q'),
8    spinTitle = webviewHelper.spinTitle,
9    spinWait = require('../../../helpers/spin.js').spinWait,
10    skip = webviewHelper.skip;
11
12module.exports = function (desired) {
13
14  describe('basics', function () {
15    var driver;
16    setup(this, desired, {'no-reset': true}).then(function (d) { driver = d; });
17
18    beforeEach(function (done) {
19      loadWebView(desired, driver).nodeify(done);
20    });
21    it('should find a web element in the web view', function (done) {
22      driver
23        .elementById('i_am_an_id').should.eventually.exist
24        .nodeify(done);
25    });
26    it('should find multiple web elements in the web view', function (done) {
27      driver
28        .elementsByTagName('a').should.eventually.have.length.above(0)
29        .nodeify(done);
30    });
31    it('should fail gracefully to find multiple missing web elements in the web view', function (done) {
32      driver
33        .elementsByTagName('blar').should.eventually.have.length(0)
34        .nodeify(done);
35    });
36    it('should find element from another element', function (done) {
37      driver
38        .elementByClassName('border')
39        .elementByXPath('>', './form').should.eventually.exist
40        .nodeify(done);
41    });
42    it('should be able to click links', function (done) {
43      driver
44        .elementByLinkText('i am a link').click()
45        .then(function () { return spinTitle('I am another page title', driver); })
46        .nodeify(done);
47    });
48
49    it('should retrieve an element attribute', function (done) {
50      driver
51        .elementById('i_am_an_id')
52          .getAttribute("id").should.become('i_am_an_id')
53        .elementById('i_am_an_id')
54          .getAttribute("blar").should.not.eventually.exist
55        .nodeify(done);
56    });
57    it('should retrieve implicit attributes', function (done) {
58      driver
59        .elementsByTagName('option')
60        .then(function (els) {
61          els.should.have.length(3);
62          return els[2].getAttribute('index').should.become('2');
63        }).nodeify(done);
64    });
65    it('should retrieve an element text', function (done) {
66      driver
67        .elementById('i_am_an_id').text().should.become('I am a div')
68        .nodeify(done);
69    });
70    it('should check if two elements are equals', function (done) {
71      Q.all([
72        driver.elementById('i_am_an_id'),
73        driver.elementByTagName('div')
74      ]).then(function (els) {
75        return els[0].equals(els[1]).should.be.ok;
76      }).nodeify(done);
77    });
78    it('should return the page source', function (done) {
79      driver
80        .source()
81        .then(function (source) {
82          source.should.include('<html');
83          source.should.include('I am a page title');
84          source.should.include('i appear 3 times');
85          source.should.include('</html>');
86        }).nodeify(done);
87    });
88    it('should get current url', function (done) {
89      driver
90        .url().should.eventually.include("test/guinea-pig")
91        .nodeify(done);
92    });
93    it('should send keystrokes to specific element', function (done) {
94      driver
95        .elementById('comments')
96          .clear()
97          .sendKeys("hello world")
98          .getValue().should.become("hello world")
99        .nodeify(done);
100    });
101    it('should send keystrokes to active element', function (done) {
102      driver
103        .elementById('comments')
104          .clear()
105          .click()
106          .keys("hello world")
107        .elementById('comments')
108          .getValue().should.become("hello world")
109        .nodeify(done);
110    });
111    it('should clear element', function (done) {
112      driver
113        .elementById('comments')
114          .sendKeys("hello world")
115          .getValue().should.eventually.have.length.above(0)
116        .elementById('comments')
117          .clear()
118          .getValue().should.become("")
119        .nodeify(done);
120    });
121    it('should say whether an input is selected', function (done) {
122      driver
123        .elementById('unchecked_checkbox')
124          .selected().should.not.eventually.be.ok
125        .elementById('unchecked_checkbox')
126          .click()
127          .selected().should.eventually.be.ok
128        .nodeify(done);
129    });
130    it('should be able to retrieve css properties', function (done) {
131      driver
132        .elementById('fbemail').getComputedCss('background-color')
133          .should.become("rgba(255, 255, 255, 1)")
134        .nodeify(done);
135    });
136    it('should retrieve an element size', function (done) {
137      driver
138        .elementById('i_am_an_id').getSize()
139        .then(function (size) {
140          size.width.should.be.above(0);
141          size.height.should.be.above(0);
142        }).nodeify(done);
143    });
144    it('should get location of an element', function (done) {
145      driver
146        .elementById('fbemail')
147          .getLocation()
148        .then(function (loc) {
149          loc.x.should.be.above(0);
150          loc.y.should.be.above(0);
151        }).nodeify(done);
152    });
153    it('should retrieve tag name of an element', function (done) {
154      driver
155        .elementById('fbemail').getTagName().should.become("input")
156        .elementByCss("a").getTagName().should.become("a")
157        .nodeify(done);
158    });
159
160    it('should retrieve a window size @skip-chrome', function (done) {
161      driver
162        .getWindowSize()
163        .then(
164          function (size) {
165            size.height.should.be.above(0);
166            size.width.should.be.above(0);
167          }).nodeify(done);
168    });
169    it('should move to an arbitrary x-y element and click on it', function (done) {
170      driver.elementByLinkText('i am a link')
171        .moveTo(5, 15)
172        .click()
173      .then(function () { return spinTitle("I am another page title", driver); })
174      .nodeify(done);
175    });
176    it('should submit a form', function (done) {
177      driver
178        .elementById('comments')
179          .sendKeys('This is a comment')
180          .submit()
181        .then(function () {
182          return spinWait(function () {
183            return driver
184              .elementById('your_comments')
185              .text()
186              .should.become('Your comments: This is a comment');
187          });
188        }
189      ).nodeify(done);
190    });
191    it('should return true when the element is displayed', function (done) {
192      driver
193        .elementByLinkText('i am a link')
194          .isDisplayed().should.eventually.be.ok
195        .nodeify(done);
196    });
197    it('should return false when the element is not displayed', function (done) {
198      driver
199        .elementById('invisible div')
200          .isDisplayed().should.not.eventually.be.ok
201        .nodeify(done);
202    });
203    it('should return true when the element is enabled', function (done) {
204      driver
205        .elementByLinkText('i am a link')
206          .isEnabled().should.eventually.be.ok
207        .nodeify(done);
208    });
209    it('should return false when the element is not enabled', function (done) {
210      driver
211        .execute("$('#fbemail').attr('disabled', 'disabled');")
212        .elementById('fbemail').isEnabled().should.not.eventually.be.ok
213        .nodeify(done);
214    });
215    it("should return the active element", function (done) {
216      var testText = "hi there";
217      driver
218        .elementById('i_am_a_textbox').sendKeys(testText)
219        .active().getValue().should.become(testText)
220        .nodeify(done);
221    });
222    it('should properly navigate to anchor', function (done) {
223      driver
224        .url().then(function (curl) {
225          return driver.get(curl);
226        }).nodeify(done);
227    });
228    it('should be able to refresh', function (done) {
229      driver.refresh()
230      .nodeify(done);
231    });
232    it('should be able to get performance logs', function (done) {
233      if (!isChrome(desired)) return skip(
234        "Performance logs aren't available except in Chrome", done);
235      driver
236        .logTypes()
237          .should.eventually.include('performance')
238        .log('performance').then(function (logs) {
239          logs.length.should.be.above(0);
240        }).nodeify(done);
241    });
242  });
243};
244
Full Screen

find-element-specs.js

Source: find-element-specs.js Github

copy
1"use strict";
2
3var setup = require("../../common/setup-base")
4  , desired = require('./desired')
5  , Q = require("q")
6  , _ = require("underscore")
7  , spinWait = require("../../../helpers/spin.js").spinWait;
8
9describe('uicatalog - find element @skip-ios7up', function () {
10  var driver;
11  setup(this, desired).then(function (d) { driver = d; });
12
13  it('should find a single element by id @skip-ios6', function (done) {
14    // ById don't seem to be supported since appium 1.0
15    driver
16      .elementById('ButtonsExplain')
17        .should.eventually.exist
18      .nodeify(done);
19  });
20
21  it('should find a single element by id wrapped in array for multi @skip-ios6', function (done) {
22    // ById don't seem to be supported since appium 1.0
23    driver
24      .elementsById('ButtonsExplain')
25      .then(function (els) {
26        els.length.should.equal(1);
27      })
28      .nodeify(done);
29  });
30
31  it('should find a single element using elementByName', function (done) {
32    driver
33      .elementByName('UICatalog').then(function (el) {
34        el.should.exist;
35      }).nodeify(done);
36  });
37  it('should find an element within descendants', function (done) {
38    driver
39      .elementByClassName('UIATableView').then(function (el) {
40        el.should.exist;
41        return el.elementByClassName('UIAStaticText').getAttribute('name')
42          .should.become("Buttons, Various uses of UIButton");
43      }).nodeify(done);
44  });
45
46  it('should not find an element not within itself', function (done) {
47    driver
48      .elementByClassName('UIATableView').then(function (el) {
49        el.should.exist;
50        return el.elementByClassName('UIANavigationBar')
51          .should.be.rejectedWith(/status: 7/);
52      }).nodeify(done);
53  });
54
55  it('should find some elements within itself', function (done) {
56    driver
57      .elementByClassName('UIATableCell').then(function (el) {
58        el.should.exist;
59        return el.elementsByClassName('UIAStaticText')
60          .should.eventually.have.length(1);
61      }).nodeify(done);
62  });
63
64  it('should not find elements not within itself', function (done) {
65    driver
66      .elementByClassName('UIATableCell').then(function (el) {
67        el.should.exist;
68        el.elementsByClassName('UIANavigationBar')
69          .should.eventually.have.length(0);
70      }).nodeify(done);
71  });
72
73  it('should not allow found elements to be mixed up', function (done) {
74    var el1, el2, el1Name, el2Name;
75    driver
76      .elementByClassName('UIATableCell')
77      .then(function (el) {
78        el1 = el;
79        return el1.getAttribute('name').then(function (name) {
80          el1Name = name;
81        });
82      })
83      .elementByClassName('UIATableCell')
84        .click()
85      .delay(1000)
86      .elementByClassName('UIATableCell')
87      .then(function (el) {
88        el2 = el;
89        el2.value.should.not.equal(el1.value);
90        return el2.getAttribute('name').then(function (name) {
91          el2Name = name;
92        });
93      }).then(function () {
94        el1.value.should.not.equal(el2.value);
95        el1Name.should.not.equal(el2Name);
96        // el1 is gone, so it doesn't have a name anymore
97        return el1.getAttribute('name')
98          .should.eventually.equal("");
99      })
100      .back()
101      .nodeify(done);
102  });
103
104  // TODO: CI hangs on those disabling, investigation needed
105  describe('find elements using accessibility id locator strategy @skip-ci', function () {
106    it('should find an element by name', function (done) {
107      driver.element('accessibility id', 'UICatalog').then(function (el) {
108        el.should.exist;
109      }).nodeify(done);
110    });
111    it('should find a deeply nested element by name', function (done) {
112      driver
113        .element('accessibility id', 'Toolbar, Uses of UIToolbar').then(function (el) {
114        el.should.exist;
115      }).nodeify(done);
116    });
117    it('should find an element by name beneath another element', function (done) {
118      driver.element('accessibility id', 'Empty list').then(function (el) {
119        el.element('accessibility id', 'Controls, Various uses of UIControl').then(function (innerEl) {
120          innerEl.should.exist;
121        }).nodeify(done);
122      });
123    });
124    it('should return an array of one element if the plural "elements" is used', function (done) {
125      driver.elements('accessibility id', 'UICatalog').then(function (els) {
126        els.length.should.equal(2);
127      }).nodeify(done);
128    });
129  });
130
131  describe('findElementsByClassName', function () {
132    it('should return all image elements with internally generated ids', function (done) {
133      driver.elementsByClassName('UIAImage').then(function (els) {
134        els.length.should.be.above(0);
135        _(els).each(function (el) {
136          el.should.exist;
137        });
138      }).nodeify(done);
139    });
140  });
141
142  describe('findElementsByClassName textfield case', function () {
143    after(function (done) {
144      driver.clickBack()
145      .nodeify(done);
146    });
147    it('should find only one textfield', function (done) {
148      driver
149        .elementsByClassName('UIATableCell').then(function (els) { return els[2]; })
150          .click()
151        .elementByName('Rounded')
152        .elementsByClassName('>', 'UIATextField')
153          .should.eventually.have.length(1)
154        .nodeify(done);
155    });
156  });
157
158  describe('findElement(s) containing name', function () {
159    after(function (done) {
160      driver.clickBack()
161      .nodeify(done);
162    });
163
164    it('should find one element', function (done) {
165      driver
166        .elementsByClassName('UIATableCell').then(function (els) { return els[2]; })
167          .click()
168        .elementByName('*Rounded*').getAttribute('name')
169          .should.become('UITextField Rounded')
170        .nodeify(done);
171    });
172
173    it('should find several element', function (done) {
174      driver
175        .elementsByClassName('UIATableCell').then(function (els) { return els[2]; })
176          .click()
177        .elementsByName('*Rounded*')
178          .should.eventually.have.length.above(3)
179        .nodeify(done);
180    });
181  });
182
183
184  describe('findElement(s)ByXPath', function () {
185    var setupXpath = function (driver) {
186      return driver.elementByClassName('UIATableCell').click();
187    };
188
189    if (process.env.FAST_TESTS) {
190      afterEach(function (done) {
191        driver
192          .back()
193          .nodeify(done);
194      });
195    }
196
197    it('should return the last button', function (done) {
198      driver
199        .resolve(setupXpath(driver))
200        .elementByXPath("//UIAButton[last()]").text()
201          .should.become("Add contact")
202        .nodeify(done);
203    });
204    it('should return a single element', function (done) {
205      driver
206        .resolve(setupXpath(driver))
207        .elementByXPath("//UIAButton").text()
208          .should.become("Back")
209        .nodeify(done);
210    });
211    it('should return multiple elements', function (done) {
212      driver
213        .resolve(setupXpath(driver))
214        .elementsByXPath("//UIAButton")
215          .should.eventually.have.length.above(5)
216        .nodeify(done);
217    });
218    it('should filter by name', function (done) {
219      driver
220        .resolve(setupXpath(driver))
221        .elementByXPath("//UIAButton[@name='Rounded']").text()
222          .should.become("Rounded")
223        .nodeify(done);
224    });
225    it('should know how to restrict root-level elements', function (done) {
226      driver
227        .resolve(setupXpath(driver))
228        .elementByXPath("/UIAButton")
229          .should.be.rejectedWith(/status: 7/)
230        .nodeify(done);
231    });
232    it('should search an extended path by child', function (done) {
233      driver
234        .resolve(setupXpath(driver))
235        .then(function () {
236          return spinWait(function () {
237            return driver.elementByXPath("//UIANavigationBar/UIAStaticText")
238              .text().should.become('Buttons');
239          });
240        }).nodeify(done);
241    });
242    it('should search an extended path by descendant', function (done) {
243      driver
244        .resolve(setupXpath(driver))
245        .elementsByXPath("//UIATableCell//UIAButton").then(function (els) {
246          return Q.all(_(els).map(function (el) { return el.text(); }));
247        }).then(function (texts) {
248          texts.should.not.include("Button");
249          texts.should.include("Gray");
250        }).nodeify(done);
251    });
252    it('should filter by indices', function (done) {
253      driver
254        .resolve(setupXpath(driver))
255        .then(function () {
256          return spinWait(function () {
257            return driver.elementByXPath("//UIATableCell[2]//UIAStaticText[1]").getAttribute('name')
258                .should.become("ButtonsViewController.m:\r(UIButton *)grayButton");
259          });
260        }).nodeify(done);
261    });
262    it('should filter by partial text', function (done) {
263      driver
264        .resolve(setupXpath(driver))
265        .elementByXPath("//UIATableCell//UIAButton[contains(@name, 'Gr')]").text()
266          .should.become("Gray")
267        .nodeify(done);
268    });
269  });
270
271  describe('FindElement(s)ByUIAutomation', function () {
272    var byUIA = '-ios uiautomation';
273
274    var filterDisplayed = function (els) {
275      return Q.all(_.map(els, function (el) { return el.isDisplayed(); }))
276        .then(function (res) { return _.filter(els, function (el, i) { return res[i]; }); });
277    };
278
279    before(function (done) {
280      driver
281        .element(byUIA, '.navigationBars()[0]')
282          .getAttribute('name').then(function (name) {
283            if (name !== 'UICatalog') {
284              return driver.back().delay(2000);
285            } else {
286              return Q.delay(500);
287            }
288          }
289        ).nodeify(done);
290    });
291
292    it('should process most basic UIAutomation query', function (done) {
293      driver
294        .elements(byUIA, '.elements()').then(filterDisplayed)
295          .should.eventually.have.length(2)
296        .nodeify(done);
297    });
298    it('should process UIAutomation queries if user leaves out the first period', function (done) {
299      driver
300        .elements(byUIA, '$.mainWindow().elements()').then(filterDisplayed)
301          .should.eventually.have.length(2)
302        .nodeify(done);
303    });
304    it('should get a single element', function (done) {
305      driver.element(byUIA, '.elements()[0]').getAttribute('name')
306        .should.become('UICatalog')
307      .nodeify(done);
308    });
309    it('should get a single element', function (done) {
310      driver.element(byUIA, '.elements()[1]').getAttribute('name')
311        .should.become('Empty list')
312      .nodeify(done);
313    });
314    it('should get single element as array', function (done) {
315      driver
316        .elements(byUIA, '.tableViews()[0]')
317          .should.eventually.have.length(1)
318        .nodeify(done);
319    });
320    it('should find elements by index multiple times', function (done) {
321      driver.element(byUIA, '.elements()[1].cells()[2]').getAttribute('name')
322        .should.become('TextFields, Uses of UITextField')
323      .nodeify(done);
324    });
325    it('should find elements by name', function (done) {
326      driver.element(byUIA, '.elements()["UICatalog"]').getAttribute('name')
327        .should.become('UICatalog')
328      .nodeify(done);
329    });
330    it('should find elements by name and index', function (done) {
331      driver.element(byUIA, '.elements()["Empty list"].cells()[3]').getAttribute('name')
332        .should.become('SearchBar, Use of UISearchBar')
333      .nodeify(done);
334    });
335    describe('start from a given context instead of root target', function () {
336      it('should process a simple query', function (done) {
337        driver.element(byUIA, '.elements()[1]').then(function (el) {
338          el
339            .elements(byUIA, '.elements()')
340              .should.eventually.have.length(12)
341            .nodeify(done);
342        });
343      });
344      it('should find elements by name', function (done) {
345        driver.element(byUIA, '.elements()[1]').then(function (el) {
346          el
347          .element(byUIA, '.elements()["Buttons, Various uses of UIButton"]')
348            .should.eventually.exist
349          .nodeify(done);
350        });
351      });
352    });
353  });
354});
355
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Appium on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)