Best JavaScript code snippet using webdriverio-monorepo
test.js
Source:test.js  
...33  await driver.execute('flutter:clearTimeline');34  await driver.execute('flutter:forceGC');35  // empty fields test /////////////////////////////////////////////////////////36  var logs = [];37  await driver.elementSendKeys(find.byValueKey('fname'), '');38  await driver.elementSendKeys(find.byValueKey('lname'), '');39  await driver.elementClick(find.byValueKey('cityDropdown'));40  await driver.elementClick(find.byText('Artvin'));41  try {42    // send button43    await driver.execute('flutter:waitFor', find.byValueKey('submit'), {});44    await driver.elementClick(find.byValueKey('submit'));45    logs.push("Send button was clicked");46    logs.push("TEST FAILED");47  }48  catch(err) {49    logs.push("Send button did not appear");50    logs.push("TEST PASSED");51  }52  logger('Empty fields test', logs);53  // Incorrect fields test /////////////////////////////////////////////////////////54  var logs = [];55  await driver.elementSendKeys(find.byValueKey('fname'), '172hhh');56  await driver.elementSendKeys(find.byValueKey('lname'), 'bf29ff$');57  try {58    // send button59    await driver.execute('flutter:waitFor', find.byValueKey('submit'), {});60    await driver.elementClick(find.byValueKey('submit'));61    logs.push("Send button was clicked");62    logs.push("TEST FAILED");63  }64  catch(err) {65    logs.push("Send button did not appear");66    logs.push("TEST PASSED");67  }68  logger('Incorrect fields test', logs);69  // Incorrect birth date test /////////////////////////////////////////////////////////70  var logs = [];71  await driver.elementClick(find.byValueKey('dateField'));72  var today = new Date();73  await driver.elementClick(find.byText(String(today.getDate()+1)));74  await driver.elementClick(find.byText('OK'));75  var today = new Date();76  77  try {78    // send button79    await driver.execute('flutter:waitFor', find.byValueKey('submit'), {});80    await driver.elementClick(find.byValueKey('submit'));81    logs.push("Send button was clicked");82    logs.push("TEST FAILED");83  }84  catch(err) {85    logs.push("Send button did not appear");86    logs.push("TEST PASSED");87  }88  logger('Incorrect birth date test', logs);89  // Successful submit test /////////////////////////////////////////////////////////90  var logs = [];91  // Clear and enter correct names92  await driver.elementClear(find.byValueKey('fname'));93  await driver.elementClear(find.byValueKey('lname'));94  await driver.elementSendKeys(find.byValueKey('fname'), 'John');95  await driver.elementSendKeys(find.byValueKey('lname'), 'Doe');96  // Choose day97  await driver.elementClick(find.byValueKey('dateField'));98  var today = new Date();99  await driver.elementClick(find.byText(String(today.getDate())));100  await driver.elementClick(find.byText('OK'));101  // Choose city102  await driver.elementClick(find.byValueKey('cityDropdown'));103  await driver.elementClick(find.byText('Artvin'));104  // Choose gender105  await driver.elementClick(find.byValueKey('genderDropdown'));106  await driver.elementClick(find.byText('Male'));107  // Choose vaccine108  await driver.elementClick(find.byValueKey('vaccineDropdown'));109  await driver.elementClick(find.byText('Moderna'));110  // Enter side effects111  await driver.elementClear(find.byValueKey('sideEffects'));112  await driver.elementSendKeys(find.byValueKey('sideEffects'), 'Not Applicable');113  // Submit114  try {115    // send button116    await driver.execute('flutter:waitFor', find.byValueKey('submit'));117    await driver.elementClick(find.byValueKey('submit'));118    logs.push("Send button was clicked");119    logs.push("TEST PASSED");120  }121  catch(err) {122    logs.push("Send button did not appear");123    logs.push("TEST FAILED");124  }125  logger('Successful submission test', logs);126  // Switch context /////////////////////////////////////////////////////////127  var logs = [];128  await driver.elementClear(find.byValueKey('fname'));129  await driver.elementClear(find.byValueKey('lname'));130  await driver.elementSendKeys(find.byValueKey('fname'), 'Jane');131  await driver.elementSendKeys(find.byValueKey('lname'), 'Maybe');132  await driver.switchContext('NATIVE_APP');133  await driver.background(0.7);134  await driver.switchContext('FLUTTER');135  try{136    assert.strictEqual(await driver.getElementText(find.byValueKey('fname')), 'Jane');137    assert.strictEqual(await driver.getElementText(find.byValueKey('lname')), 'Maybe');138    logs.push("Context Switched and text remained in fields");139    logs.push("TEST PASSED");140  }141  catch(err) {142    logs.push("Context switch did not work and text fields changed");143    logs.push("TEST FAILED");144  }145  logger('Context switch test', logs);146  // Orientation test /////////////////////////////////////////////////////////147  var logs = [];148  await driver.elementClick(find.byValueKey('orientation'));149  // Clear and enter correct names150  await driver.elementClear(find.byValueKey('fname'));151  await driver.elementClear(find.byValueKey('lname'));152  await driver.elementSendKeys(find.byValueKey('fname'), 'John');153  await driver.elementSendKeys(find.byValueKey('lname'), 'Doe');154  await driver.execute('flutter:scrollIntoView',find.byValueKey('dateField'), {alignment: 0.1})155  // Choose day156  await driver.elementClick(find.byValueKey('dateField'));157  var today = new Date();158  await driver.elementClick(find.byText(String(today.getDate())));159  await driver.elementClick(find.byText('OK'));160  await driver.execute('flutter:scrollIntoView',find.byValueKey('cityDropdown'), {alignment: 0.1})  161  // Choose city162  await driver.elementClick(find.byValueKey('cityDropdown'));163  await driver.elementClick(find.byText('Adana'));164  await driver.execute('flutter:scrollIntoView',find.byValueKey('genderDropdown'), {alignment: 0.1})  165  // Choose gender166  await driver.elementClick(find.byValueKey('genderDropdown'));167  await driver.elementClick(find.byText('Male'));168  await driver.execute('flutter:scrollIntoView',find.byValueKey('vaccineDropdown'), {alignment: 0.1})  169  // Choose vaccine170  await driver.elementClick(find.byValueKey('vaccineDropdown'));171  await driver.elementClick(find.byText('Moderna'));172  await driver.execute('flutter:scrollIntoView',find.byValueKey('sideEffects'), {alignment: 0.1})  173  // Enter side effects174  await driver.elementClear(find.byValueKey('sideEffects'));175  await driver.elementSendKeys(find.byValueKey('sideEffects'), 'Not Applicable');176  await driver.execute('flutter:scrollIntoView',find.byValueKey('submit'), {alignment: 0.1})  177  await driver.elementClick(find.byValueKey('orientation'));178  // Submit179  try {180    // send button181    await driver.execute('flutter:waitFor', find.byValueKey('submit'));182    await driver.elementClick(find.byValueKey('submit'));183    logs.push("Send button was clicked with changed orientation");184    logs.push("TEST PASSED");185  }186  catch(err) {187    logs.push("Send button did not appear with changed orientation");188    logs.push("TEST FAILED");189  }...index.js
Source:index.js  
...51  const checkBox = find.byType('CheckboxListTile');52  // Test Case 1 -- Checking whether all the fields are field with correct data whether button appears or not53  console.log("1st Test Case -- ")54  await driver.execute('flutter:waitFor', find.byValueKey('textFieldName'));55  await driver.elementSendKeys(nameText, validName);56  await enterBirthday(driver, '12');57  await driver.elementSendKeys(cityText, validCity);58  await driver.elementSendKeys(vaccineText, validVaccine);59  await driver.elementClick(radioFemale);60  await driver.elementClick(checkBox);61  await driver.execute('flutter:waitFor', sideEffectText); 62  await driver.elementSendKeys(sideEffectText, validSideEffect);63  try {64    await driver.execute('flutter:waitFor', find.byValueKey('buttonSend'));65    console.log('First test case passed.');66  } catch(exception) {67    console.log('First test case not passed.');68    return exception;69  }70  await driver.elementClick(find.byValueKey('buttonSend'));71  await driver.execute('flutter:waitFor', find.byText('Okay') );72  73  await driver.elementClick(find.byText('Okay'));74  // Test Case 2 -- Checking the application with invalid data ( whether we will be able to see send button)75  console.log("2nd Test Case -- ")76  await driver.execute('flutter:waitFor', find.byValueKey('textFieldName'));77  await driver.elementSendKeys(nameText, invalidName);78  await driver.elementSendKeys(cityText, invalidCity);79  await driver.elementSendKeys(vaccineText, validVaccine);80  try {81    await driver.execute(find.byValueKey('buttonSend'));82    console.log('Second test case not passed.');83  } catch(exception) {84    console.log("Second Test case passed!");85  }86  // Test Case 3 -- Checking whether survey fields get cleared after the send button is clicked87  console.log("3rd Test Case -- ")88  await driver.execute('flutter:waitFor', find.byValueKey('textFieldName'));89  await driver.elementSendKeys(nameText, validName);90  await enterBirthday(driver, '7');91  await driver.elementSendKeys(cityText, validCity);92  await driver.elementSendKeys(vaccineText, validVaccine);93  await driver.elementClick(radioFemale);94  await driver.elementClick(checkBox);95  await driver.elementSendKeys(sideEffectText, validSideEffect);96  await driver.execute('flutter:waitFor', find.byValueKey('buttonSend'));97  await driver.elementClick(find.byValueKey('buttonSend'));98  await driver.execute('flutter:waitFor', find.byText('Okay') );99  await driver.elementClick(find.byText('Okay'));100  try {101    assert.strictEqual(await driver.getElementText(nameText), '');102    assert.strictEqual(await driver.getElementText(birthdayText), '');103    assert.strictEqual(await driver.getElementText(cityText),'');104    assert.strictEqual(await driver.getElementText(vaccineText),'');105    console.log('Third test case not passed.')106  }catch (e) {107    console.log('Third test case not passed.');108    return (e);109  }110  // Test Case 4 -- Check if a text field appears when checking the âyesâ checkbox button for side effects.111  console.log("4th Test Case -- ")112  await driver.elementClick(checkBox);113  await driver.execute('flutter:waitFor', sideEffectText); 114  try {115    assert.strictEqual(await driver.getElementText(sideEffectText), '');116    console.log('Fourt test case passed.')117  }catch (e) {118    console.log('Fourt test case not passed.')119  }120  121  // Test Case 5 -- Check if a text appears after a successful submission informing the end-user that the given data is saved successfully.122  console.log("5th Test Case -- ")123  await driver.execute('flutter:waitFor', nameText);124  await driver.elementSendKeys(nameText, validName);125  await driver.elementSendKeys(cityText, validCity);126  await enterBirthday(driver, '1');127  await driver.elementSendKeys(vaccineText, validVaccine);128  await driver.elementClick(radioFemale);129  await driver.execute('flutter:waitFor', sideEffectText); 130  await driver.elementSendKeys(sideEffectText, validSideEffect);131  await driver.execute('flutter:waitFor', find.byValueKey('buttonSend'));132  await driver.elementClick(find.byValueKey('buttonSend'));133  await driver.execute('flutter:waitFor', find.byText('Okay') );134  try {135    await driver.elementClick(find.byText('Okay'));136    console.log('Fifth test case passed.');137  }catch (e) {138    console.log('Fifth test case not passed.');139  }140  driver.deleteSession();141})();142const enterBirthday = async (driver, day) => {143  const birthdayText = find.byValueKey('textFieldBirthday');144  await driver.execute('flutter:waitFor', birthdayText );...setup.js
Source:setup.js  
...128    await this.client.elementClick(getElementId(elementRef));129  }130  async elementSendKeyText(elementRef, value) {131    if(this.client.isW3C) {132      await this.client.elementSendKeys(getElementId(elementRef), value);133    }134    else {135      await this.client.elementSendKeys(getElementId(elementRef), value.split(''));136    }137  }138  async elementSendKeys(elementRef, keys) {139    if(this.client.isW3C) {140      await this.client.elementSendKeys(getElementId(elementRef), keys.join(''));141    }142    else {143      await this.client.elementSendKeys(getElementId(elementRef), keys);144    }145  }146}147const withWebdriverClient = (testDefFn) => {148  setupBrowserstackLocal();149  for(let capabilities of capabilitiesList) {150    let wd = null;151    describe(capabilities.browserName, () => {152      before(async () => {153        const client = await WebDriver.newSession({154          path: '/wd/hub',155          hostname: 'hub-cloud.browserstack.com',156          port: 80,157          capabilities: capabilities,...autocomplete.test.js
Source:autocomplete.test.js  
...28      await wd.elementSendKeyText(inputElm, '4, 24');29      assert.strictEqual(await wd.getElementValue(inputElm), 'Rentemestervej 4, 24');30      await wd.waitForElementByRegex('li.dawa-selected',31        /Rentemestervej.4,.*2400.København.NV/);32      await wd.elementSendKeys(inputElm, [Keys.RETURN]);33      await wd.waitUntil(async () => await wd.getElementValue(inputElm) === 'Rentemestervej 4, 2400 København NV');34    });35    it('Hvis der vælges en adgangsadresse med flere adresser, udfyldes tekst og caretpos korrekt', async () => {36      const wd = wdFn();37      const inputElm = await wd.findElementByCss('#autocomplete-default');38      // click to set focus on input39      await wd.elementClick(inputElm);40      await wd.elementSendKeyText(inputElm, 'margrethepladsen');41      const adgadrSuggestionElm = await wd.waitForElementByRegex(42        'li.dawa-autocomplete-suggestion',43        /Margrethepladsen.4,.8000.Aarhus.C/);44      await wd.elementClick(adgadrSuggestionElm);45      await wd.waitUntil(async () => await wd.getElementValue(inputElm) === 'Margrethepladsen 4, , 8000 Aarhus C');46      const caretPos = await wd.getElementProperty(inputElm, 'selectionStart');47      assert.strictEqual(caretPos, 'Margrethepladsen\u00a04, '.length);48    });49    it('Kan navigere autocomplete med keyboard', async () => {50      const wd = wdFn();51      const inputElm = await wd.findElementByCss('#autocomplete-default');52      await wd.elementClick(inputElm);53      await wd.elementSendKeyText(inputElm, 'margrethep');54      await wd.waitForElementByRegex('li.dawa-selected', /Margretheparken/);55      await wd.elementSendKeys(inputElm, [Keys.DOWN_ARROW]);56      await wd.waitForElementByRegex('li.dawa-selected', /Margrethepladsen/);57      // key down again should wrap to top58      await wd.elementSendKeys(inputElm, [Keys.DOWN_ARROW]);59      await wd.waitForElementByRegex('li.dawa-selected', /Margretheparken/);60      // key up should wrap to bottom61      await wd.elementSendKeys(inputElm, [Keys.UP_ARROW]);62      await wd.waitForElementByRegex('li.dawa-selected', /Margrethepladsen/);63      // key up should wrap to bottom64      await wd.elementSendKeys(inputElm, [Keys.UP_ARROW]);65      await wd.waitForElementByRegex('li.dawa-selected', /Margretheparken/);66    });67  });...Signup.spec.js
Source:Signup.spec.js  
...18    it('Signup with invalid age', async () =>{19      await driver.elementClick(SignupPOM.signupButtonPOM() );20      await driver.elementClick(SignupPOM.emailSignupButtonPOM() );21  22      await driver.elementSendKeys(SignupPOM.ageTextBoxPOM(),"10");23  24      await driver.elementClick(SignupPOM.ageNextButtonPOM() );25  26      const assertion = await ScreenContains("next")27      driver.waitUntil (()=>assertion)28    })29  30    it('Signup with invalid email', async () => {31      32      await driver.elementClick( SignupPOM.signupButtonPOM() );33      await driver.elementClick( SignupPOM.emailSignupButtonPOM() );34  35      await driver.elementSendKeys( SignupPOM.ageTextBoxPOM(),"20");36  37      await driver.elementClick( SignupPOM.ageNextButtonPOM() );38      39      await driver.elementSendKeys( SignupPOM.nameTextBoxPOM(),"ssthsrth2lm");40  41      await driver.elementSendKeys( SignupPOM.emailTextBoxPOM(),"ziadgmail.com");42  43      await driver.elementSendKeys( SignupPOM.passwordTextBoxPOM(),"Qwer1234##");44      await driver.elementClick( SignupPOM.signupDoneButtonPOM() );45      46    })47  48    it('Signup with invalid blog name', async () => {49      50      await driver.elementClick( SignupPOM.signupButtonPOM() );51      await driver.elementClick( SignupPOM.emailSignupButtonPOM() );52  53      await driver.elementSendKeys( SignupPOM.ageTextBoxPOM(),"20");54  55      await driver.elementClick( SignupPOM.ageNextButtonPOM() );56  57      await driver.elementSendKeys( SignupPOM.emailTextBoxPOM(),"ziad@gmail.com");58  59      await driver.elementSendKeys( SignupPOM.passwordTextBoxPOM(),"Qwer1234##");60      await driver.elementClick( SignupPOM.signupDoneButtonPOM() );61    })62  63    it('Signup with invalid password name', async () => {64  65      await driver.elementClick( SignupPOM.signupButtonPOM() );66      await driver.elementClick( SignupPOM.emailSignupButtonPOM() );67  68      await driver.elementSendKeys( SignupPOM.ageTextBoxPOM(),"20");69  70      await driver.elementClick( SignupPOM.ageNextButtonPOM() );71      72      await driver.elementSendKeys( SignupPOM.nameTextBoxPOM(),"ssthsrth2lm");73  74      await driver.elementSendKeys( SignupPOM.emailTextBoxPOM(),"ziad@gmail.com");75  76      await driver.elementSendKeys( SignupPOM.passwordTextBoxPOM(),"Qw");77      await driver.elementClick( SignupPOM.signupDoneButtonPOM() );78  79    })80  81    it('Full valid test', async () => {82      83      await driver.elementClick( SignupPOM.signupButtonPOM() );84      await driver.elementClick( SignupPOM.emailSignupButtonPOM() );85  86      await driver.elementSendKeys( SignupPOM.ageTextBoxPOM(),"20");87  88      await driver.elementClick( SignupPOM.ageNextButtonPOM() );89      90      await driver.elementSendKeys( SignupPOM.nameTextBoxPOM(),"fdbfdb");91  92      await driver.elementSendKeys( SignupPOM.emailTextBoxPOM(),"ziadfgsg@gmail.com");93  94      await driver.elementSendKeys( SignupPOM.passwordTextBoxPOM(),"Qwfdgs131$#!");95      await driver.elementClick( SignupPOM.signupDoneButtonPOM() );96    })...webdriver.js
Source:webdriver.js  
...41  }42  async function elementClick(element) {43    await client.elementClick(Object.values(element)[0]);44  }45  async function elementSendKeys(element, value) {46    if (driver.browserName === 'chrome') {47      await client.elementSendKeys(Object.values(element)[0], value);48    } else {49      await client.elementSendKeys(Object.values(element)[0], value[0]);50    }51  }52  async function elementByXPath(value) {53    return await client.findElements('xpath', value);54  }55  async function maximizeWindow() {56    try {57      await client.maximizeWindow();58    } catch (error) {59      console.log(error);60    }61  }62  const { addPromiseChainMethod } = wd;63  addPromiseChainMethod('initDriver', initDriver);...elementSendKeys.js
Source:elementSendKeys.js  
...12    if (options.singleElementSelections) {13      await checkSingleElement(client, selector)14    }15    return findElement(client, selector)16      .then(element => client.elementSendKeys(element, text))17  }...element-send-keys.js
Source:element-send-keys.js  
1import Endpoint from '..'2import { MissingCommandParameters } from '../../utils/errors.js'3class ElementSendKeys extends Endpoint {4  constructor (args) {5    let [elementId, arr] = args6    if (!(arr instanceof Array)) {7      throw MissingCommandParameters(`second argument expected to be Array, got ${arr}`)8    }9    let str = arr.join('')10    super([elementId, str])11  }12  static create (req) {13    return new ElementSendKeys([req.params.id, req.body.value])14  }15}16ElementSendKeys.method = 'post'17ElementSendKeys.url = '/session/:sid/element/:id/value'...Using AI Code Generation
1var webdriverio = require('webdriverio');2var options = {3    desiredCapabilities: {4    }5};6    .remote(options)7    .init()8    .setValue('input[name="q"]','WebdriverIO')9    .keys('Enter')10    .getTitle().then(function(title) {11        console.log('Title was: ' + title);12    })13    .end();14Title was: WebdriverIO (Software) at DuckDuckGoUsing AI Code Generation
1const webdriverio = require('webdriverio');2const options = {3    desiredCapabilities: {4    }5};6    .remote(options)7    .init()8    .elementSendKeys('input[name=q]', 'Hello World!')9    .end();Using AI Code Generation
1const webdriverio = require('webdriverio');2const options = { desiredCapabilities: { browserName: 'chrome' } };3    .remote(options)4    .init()5    .elementSendKeys('css selector', '#search_input_react', 'Hello World')6    .end();Using AI Code Generation
1const webdriverio = require('webdriverio');2const options = { desiredCapabilities: { browserName: 'chrome' } };3const client = webdriverio.remote(options);4.init()5.setValue('#lst-ib', 'WebdriverIO')6.keys('Enter')7.pause(2000)8.getTitle().then(function(title) {9    console.log('Title is: ' + title);10})11.end();Using AI Code Generation
1describe('WebdriverIO API Demos', () => {2    it('should search for WebdriverIO', () => {3        const searchInput = $('input[type="search"]');4        searchInput.setValue('WebdriverIO');5        browser.pause(3000);6    });7});8describe('WebdriverIO API Demos', () => {9    it('should search for WebdriverIO', () => {10        const searchInput = $('input[type="search"]');11        searchInput.elementSendKeys('WebdriverIO');12        browser.pause(3000);13    });14});15describe('WebdriverIO API Demos', () => {16    it('should handle alerts', () => {17        alertButton.click();18        alertLink.click();19        alertButton2.click();20        const alertText = browser.getAlertText();Wondering what could be a next-gen browser and mobile test automation framework that is also simple and concise? Yes, that’s right, it's WebdriverIO. Since the setup is very easy to follow compared to Selenium testing configuration, you can configure the features manually thereby being the center of attraction for automation testing. Therefore the testers adopt WedriverIO to fulfill their needs of browser testing.
Learn to run automation testing with WebdriverIO tutorial. Go from a beginner to a professional automation test expert with LambdaTest WebdriverIO tutorial.
Running Your First Automation Script - Learn the steps involved to execute your first Test Automation Script using WebdriverIO since the setup is very easy to follow and the features can be configured manually.
Selenium Automation With WebdriverIO - Read more about automation testing with WebdriverIO and how it supports both browsers and mobile devices.
Browser Commands For Selenium Testing - Understand more about the barriers faced while working on your Selenium Automation Scripts in WebdriverIO, the ‘browser’ object and how to use them?
Handling Alerts & Overlay In Selenium - Learn different types of alerts faced during automation, how to handle these alerts and pops and also overlay modal in WebdriverIO.
How To Use Selenium Locators? - Understand how Webdriver uses selenium locators in a most unique way since having to choose web elements very carefully for script execution is very important to get stable test results.
Deep Selectors In Selenium WebdriverIO - The most popular automation testing framework that is extensively adopted by all the testers at a global level is WebdriverIO. Learn how you can use Deep Selectors in Selenium WebdriverIO.
Handling Dropdown In Selenium - Learn more about handling dropdowns and how it's important while performing automated browser testing.
Automated Monkey Testing with Selenium & WebdriverIO - Understand how you can leverage the amazing quality of WebdriverIO along with selenium framework to automate monkey testing of your website or web applications.
JavaScript Testing with Selenium and WebdriverIO - Speed up your Javascript testing with Selenium and WebdriverIO.
Cross Browser Testing With WebdriverIO - Learn more with this step-by-step tutorial about WebdriverIO framework and how cross-browser testing is done with WebdriverIO.
Get 100 minutes of automation test minutes FREE!!
