How to use execute_async_script method in robotframework-appiumlibrary

Best Python code snippet using robotframework-appiumlibrary_python

executing_async_javascript_tests.py

Source:executing_async_javascript_tests.py Github

copy

Full Screen

...25 yield26 driver.set_script_timeout(30)27def testShouldNotTimeoutIfCallbackInvokedImmediately(driver, pages):28 pages.load("ajaxy_page.html")29 result = driver.execute_async_script("arguments[arguments.length - 1](123);")30 assert type(result) == int31 assert 123 == result32def testShouldBeAbleToReturnJavascriptPrimitivesFromAsyncScripts_NeitherNoneNorUndefined(driver, pages):33 pages.load("ajaxy_page.html")34 assert 123 == driver.execute_async_script("arguments[arguments.length - 1](123);")35 assert "abc" == driver.execute_async_script("arguments[arguments.length - 1]('abc');")36 assert not bool(driver.execute_async_script("arguments[arguments.length - 1](false);"))37 assert bool(driver.execute_async_script("arguments[arguments.length - 1](true);"))38def testShouldBeAbleToReturnJavascriptPrimitivesFromAsyncScripts_NullAndUndefined(driver, pages):39 pages.load("ajaxy_page.html")40 assert driver.execute_async_script("arguments[arguments.length - 1](null)") is None41 assert driver.execute_async_script("arguments[arguments.length - 1]()") is None42def testShouldBeAbleToReturnAnArrayLiteralFromAnAsyncScript(driver, pages):43 pages.load("ajaxy_page.html")44 result = driver.execute_async_script("arguments[arguments.length - 1]([]);")45 assert "Expected not to be null!", result is not None46 assert type(result) == list47 assert len(result) == 048def testShouldBeAbleToReturnAnArrayObjectFromAnAsyncScript(driver, pages):49 pages.load("ajaxy_page.html")50 result = driver.execute_async_script("arguments[arguments.length - 1](new Array());")51 assert "Expected not to be null!", result is not None52 assert type(result) == list53 assert len(result) == 054def testShouldBeAbleToReturnArraysOfPrimitivesFromAsyncScripts(driver, pages):55 pages.load("ajaxy_page.html")56 result = driver.execute_async_script(57 "arguments[arguments.length - 1]([null, 123, 'abc', true, false]);")58 assert result is not None59 assert type(result) == list60 assert not bool(result.pop())61 assert bool(result.pop())62 assert "abc" == result.pop()63 assert 123 == result.pop()64 assert result.pop() is None65 assert len(result) == 066def testShouldBeAbleToReturnWebElementsFromAsyncScripts(driver, pages):67 pages.load("ajaxy_page.html")68 result = driver.execute_async_script("arguments[arguments.length - 1](document.body);")69 assert isinstance(result, WebElement)70 assert "body" == result.tag_name.lower()71def testShouldBeAbleToReturnArraysOfWebElementsFromAsyncScripts(driver, pages):72 pages.load("ajaxy_page.html")73 result = driver.execute_async_script(74 "arguments[arguments.length - 1]([document.body, document.body]);")75 assert result is not None76 assert type(result) == list77 list_ = result78 assert 2 == len(list_)79 assert isinstance(list_[0], WebElement)80 assert isinstance(list_[1], WebElement)81 assert "body" == list_[0].tag_name82 # assert list_[0] == list_[1]83def testShouldTimeoutIfScriptDoesNotInvokeCallback(driver, pages):84 pages.load("ajaxy_page.html")85 with pytest.raises(TimeoutException):86 # Script is expected to be async and explicitly callback, so this should timeout.87 driver.execute_async_script("return 1 + 2;")88def testShouldTimeoutIfScriptDoesNotInvokeCallbackWithAZeroTimeout(driver, pages):89 pages.load("ajaxy_page.html")90 with pytest.raises(TimeoutException):91 driver.execute_async_script("window.setTimeout(function() {}, 0);")92@pytest.mark.xfail_marionette93def testShouldNotTimeoutIfScriptCallsbackInsideAZeroTimeout(driver, pages):94 pages.load("ajaxy_page.html")95 driver.execute_async_script(96 """var callback = arguments[arguments.length - 1];97 window.setTimeout(function() { callback(123); }, 0)""")98def testShouldTimeoutIfScriptDoesNotInvokeCallbackWithLongTimeout(driver, pages):99 driver.set_script_timeout(0.5)100 pages.load("ajaxy_page.html")101 with pytest.raises(TimeoutException):102 driver.execute_async_script(103 """var callback = arguments[arguments.length - 1];104 window.setTimeout(callback, 1500);""")105def testShouldDetectPageLoadsWhileWaitingOnAnAsyncScriptAndReturnAnError(driver, pages):106 pages.load("ajaxy_page.html")107 driver.set_script_timeout(0.1)108 with pytest.raises(WebDriverException):109 url = pages.url("dynamic.html")110 driver.execute_async_script("window.location = '{0}';".format(url))111def testShouldCatchErrorsWhenExecutingInitialScript(driver, pages):112 pages.load("ajaxy_page.html")113 with pytest.raises(WebDriverException):114 driver.execute_async_script("throw Error('you should catch this!');")115def testShouldBeAbleToExecuteAsynchronousScripts(driver, pages):116 pages.load("ajaxy_page.html")117 typer = driver.find_element(by=By.NAME, value="typer")118 typer.send_keys("bob")119 assert "bob" == typer.get_attribute("value")120 driver.find_element(by=By.ID, value="red").click()121 driver.find_element(by=By.NAME, value="submit").click()122 assert 1 == len(driver.find_elements(by=By.TAG_NAME, value='div')), \123 "There should only be 1 DIV at this point, which is used for the butter message"124 driver.set_script_timeout(10)125 text = driver.execute_async_script(126 """var callback = arguments[arguments.length - 1];127 window.registerListener(arguments[arguments.length - 1]);""")128 assert "bob" == text129 assert "" == typer.get_attribute("value")130 assert 2 == len(driver.find_elements(by=By.TAG_NAME, value='div')), \131 "There should be 1 DIV (for the butter message) + 1 DIV (for the new label)"132def testShouldBeAbleToPassMultipleArgumentsToAsyncScripts(driver, pages):133 pages.load("ajaxy_page.html")134 result = driver.execute_async_script("""135 arguments[arguments.length - 1](arguments[0] + arguments[1]);""", 1, 2)136 assert 3 == result137# TODO DavidBurns Disabled till Java WebServer is used138# def testShouldBeAbleToMakeXMLHttpRequestsAndWaitForTheResponse(driver, pages):139# script = """140# var url = arguments[0];141# var callback = arguments[arguments.length - 1];142# // Adapted from http://www.quirksmode.org/js/xmlhttp.html143# var XMLHttpFactories = [144# function () return new XMLHttpRequest(),145# function () return new ActiveXObject('Msxml2.XMLHTTP'),146# function () return new ActiveXObject('Msxml3.XMLHTTP'),147# function () return new ActiveXObject('Microsoft.XMLHTTP')148# ];149# var xhr = false;150# while (!xhr && XMLHttpFactories.length)151# try{152# xhr = XMLHttpFactories.shift().call();153# }catch (e)154#155# if (!xhr) throw Error('unable to create XHR object');156# xhr.open('GET', url, true);157# xhr.onreadystatechange = function()158# if (xhr.readyState == 4) callback(xhr.responseText);159#160# xhr.send('');""" # empty string to stop firefox 3 from choking161#162# pages.load("ajaxy_page.html")163# driver.set_script_timeout(3)164# response = driver.execute_async_script(script, pages.sleepingPage + "?time=2")165# htm = "<html><head><title>Done</title></head><body>Slept for 2s</body></html>"...

Full Screen

Full Screen

test_execute_async_script.py

Source:test_execute_async_script.py Github

copy

Full Screen

...8 def setUp(self):9 super(TestExecuteAsyncContent, self).setUp()10 self.marionette.set_script_timeout(1000)11 def test_execute_async_simple(self):12 self.assertEqual(1, self.marionette.execute_async_script("arguments[arguments.length-1](1);"))13 def test_execute_async_ours(self):14 self.assertEqual(1, self.marionette.execute_async_script("marionetteScriptFinished(1);"))15 def test_execute_async_timeout(self):16 self.assertRaises(ScriptTimeoutException, self.marionette.execute_async_script, "var x = 1;")17 def test_execute_async_unique_timeout(self):18 self.assertEqual(2, self.marionette.execute_async_script("setTimeout(function() {marionetteScriptFinished(2);}, 2000);", script_timeout=5000))19 self.assertRaises(ScriptTimeoutException, self.marionette.execute_async_script, "setTimeout(function() {marionetteScriptFinished(3);}, 2000);")20 def test_no_timeout(self):21 self.marionette.set_script_timeout(10000)22 self.assertTrue(self.marionette.execute_async_script("""23 var callback = arguments[arguments.length - 1];24 setTimeout(function() { callback(true); }, 500);25 """))26 def test_execute_async_unload(self):27 self.marionette.set_script_timeout(5000)28 unload = """29 window.location.href = "about:blank";30 """31 self.assertRaises(JavascriptException, self.marionette.execute_async_script, unload)32 def test_check_window(self):33 self.assertTrue(self.marionette.execute_async_script("marionetteScriptFinished(window !=null && window != undefined);"))34 def test_same_context(self):35 var1 = 'testing'36 self.assertEqual(self.marionette.execute_script("""37 this.testvar = '%s';38 return this.testvar;39 """ % var1), var1)40 self.assertEqual(self.marionette.execute_async_script(41 "marionetteScriptFinished(this.testvar);", new_sandbox=False), var1)42 def test_execute_no_return(self):43 self.assertEqual(self.marionette.execute_async_script("marionetteScriptFinished()"), None)44 def test_execute_js_exception(self):45 try:46 self.marionette.execute_async_script("""47 let a = 1;48 foo(bar);49 """)50 self.assertFalse(True)51 except JavascriptException, inst:52 self.assertTrue('foo(bar)' in inst.stacktrace)53 def test_execute_async_js_exception(self):54 self.assertRaises(JavascriptException,55 self.marionette.execute_async_script, """56 var callback = arguments[arguments.length - 1];57 callback(foo());58 """)59 def test_script_finished(self):60 self.assertTrue(self.marionette.execute_async_script("""61 marionetteScriptFinished(true);62 """))63 def test_execute_permission(self):64 self.assertRaises(JavascriptException, self.marionette.execute_async_script, """65let prefs = Components.classes["@mozilla.org/preferences-service;1"]66 .getService(Components.interfaces.nsIPrefBranch);67marionetteScriptFinished(4);68""")69 def test_sandbox_reuse(self):70 # Sandboxes between `execute_script()` invocations are shared.71 self.marionette.execute_async_script("this.foobar = [23, 42];"72 "marionetteScriptFinished();")73 self.assertEqual(self.marionette.execute_async_script(74 "marionetteScriptFinished(this.foobar);", new_sandbox=False), [23, 42])75 self.marionette.execute_async_script("global.barfoo = [42, 23];"76 "marionetteScriptFinished();")77 self.assertEqual(self.marionette.execute_async_script(78 "marionetteScriptFinished(global.barfoo);", new_sandbox=False), [42, 23])79class TestExecuteAsyncChrome(TestExecuteAsyncContent):80 def setUp(self):81 super(TestExecuteAsyncChrome, self).setUp()82 self.marionette.set_context("chrome")83 def test_execute_async_unload(self):84 pass85 def test_same_context(self):86 pass87 def test_execute_permission(self):88 self.assertEqual(5, self.marionette.execute_async_script("""89var c = Components.classes;90marionetteScriptFinished(5);91"""))92 def test_sandbox_reuse(self):...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run robotframework-appiumlibrary automation tests on LambdaTest cloud grid

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful