How to use execute_javascript method in Robotframework-anywherelibrary

Best Python code snippet using robotframework-anywherelibrary

S2LExtention4Ghostdriver.py

Source:S2LExtention4Ghostdriver.py Github

copy

Full Screen

...21 lastPopupMessage = message;22 return;23 }24 """25 s2l.execute_javascript('%s' % js)26 def choose_ok_on_next_confirmation(self):27 """模拟confirmation窗口点击ok的情况,同时记录窗口中的message。28 当页面存在window.confirm()时,必须在confirmation窗口出现前使用该关键字。29 每次有新页面加载之后必须重新调用这个关键字才会生效。30 """31 s2l = BuiltIn().get_library_instance('Selenium2Library')32 js = """33 window.confirm = function(message) {34 lastPopupMessage = message;35 return true;36 }37 """38 s2l.execute_javascript('%s' % js)39 def choose_cancel_on_next_confirmation(self):40 """模拟confirmation窗口点击cancel的情况,同时记录窗口中的message。41 当页面存在window.confirm()时,必须在confirmation窗口出现前使用该关键字。42 每次有新页面加载之后必须重新调用这个关键字才会生效。 43 """44 s2l = BuiltIn().get_library_instance('Selenium2Library')45 js = """46 window.confirm = function(message) {47 lastPopupMessage = message;48 return false;49 }50 """51 s2l.execute_javascript('%s' % js)52 def input_value_on_next_prompt(self, value=None):53 """模拟prompt窗口输入value后点击ok的情况,同时记录窗口中的message。54 不输入value参数时,则模拟prompt窗口在默认值情况下被点击ok的情况。55 当页面存在window.prompt()时,必须在prompt窗口出现前使用该关键字。56 每次有新页面加载之后必须重新调用这个关键字才会生效。57 """58 s2l = BuiltIn().get_library_instance('Selenium2Library')59 if value is None:60 js = """61 window.prompt = function(message, defaultValue) {62 lastPopupMessage = message;63 return defaultValue;64 }65 """66 s2l.execute_javascript('%s' % js)67 else:68 js_prefix = """69 window.prompt = function(message, defaultValue) {70 lastPopupMessage = message;71 return '"""72 js_suffix = "';}"73 s2l.execute_javascript('%s%s%s' % (js_prefix, value, js_suffix))74 def choose_cancel_on_next_prompt(self):75 """模拟prompt窗口点击cancel的情况,同时记录窗口中的message。76 当页面存在window.prompt()时,必须在prompt窗口出现前使用该关键字。77 每次有新页面加载之后必须重新调用这个关键字才会生效。 78 """79 s2l = BuiltIn().get_library_instance('Selenium2Library')80 js = """81 window.prompt = function(message, defaultValue) {82 lastPopupMessage = message;83 return null;84 }85 """86 s2l.execute_javascript('%s' % js)87 def get_last_popup_message(self):88 """获取最后一次弹出框(alert/confirmation/prompt)中显示的文字。89 当页面存在window.alert()或者window.confirm()时,在alert/confirmation/prompt窗口出现后使用该关键字。90 只有在alert/confirmation/prompt窗口出现之前调用过这个库的`Close On Next Alert`,91 `Choose Ok On Next Confirmation`, `Choose Cancel On Next Confirmation`, 92 `Input Value On Next Prompt`, `Choose Cancel On Next Prompt`这些关键字之后,这个关键字才能起作用。93 """94 s2l = BuiltIn().get_library_instance('Selenium2Library')95 js = """96 return lastPopupMessage;97 """...

Full Screen

Full Screen

power.py

Source:power.py Github

copy

Full Screen

1# Copyright (c) 2013 The Chromium Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4import time5import numpy6from telemetry import test7from telemetry.page import page_measurement8from telemetry.core.platform.profiler.monsoon_profiler import MonsoonProfiler9def get_time():10 return "%s.%s" % (time.strftime("%H:%M:%S"), repr(time.time()).split('.')[1])11class MonsoonMeasurement(page_measurement.PageMeasurement):12 def __init__(self, *args, **kwargs):13 super(MonsoonMeasurement, self).__init__(*args, **kwargs)14 self._monsoon_profiler = None15 @property16 def results_are_the_same_on_every_page(self):17 return False18 def CustomizeBrowserOptions(self, options):19 options.no_power_monitor = True20 def _ExecuteStep(self, step, tab):21 if step["action"] == "wait":22 time.sleep(step["seconds"])23 elif step["action"] == "wait_for_javascript":24 tab.WaitForJavaScriptExpression(25 step["expression"], step.get("timeout", 60))26 elif step["action"] == "execute_javascript":27 tab.ExecuteJavaScript(step["expression"])28 def MeasurePage(self, page, tab, results):29 power_consumptions = []30 for i in range(self.repeats):31 print "Measuring power, repeat %d/%d, at %s" \32 % (i+1, self.repeats, get_time())33 self._monsoon_profiler = MonsoonProfiler(None, None, "/dev/null", None)34 for step in self.steps:35 self._ExecuteStep(step, tab)36 print "Steps measured at", get_time()37 self._monsoon_profiler.CollectProfile()38 power_consumption = self.CalculatePower(self._monsoon_profiler.results)39 power_consumptions.append(power_consumption)40 results.Add('Power consumption', 'J', power_consumptions)41 @staticmethod42 def CalculatePower(results):43 x_timestamps, currents, voltages = zip(*results)44 y_powers = [a*v for a, v in zip(currents, voltages)]45 return numpy.trapz(y_powers, x_timestamps)46 @staticmethod47 def WithSteps(name, steps, repeats):48 """Factory method for creating preconfigured multipage measurement with49 given name, repeat count and steps"""50 members = {"steps": steps, "repeats": repeats}51 return type(name, (MonsoonMeasurement,), members)52 @staticmethod53 def WithSinglePageLoadSteps(name, url, wait_after_load_event=2, repeats=20):54 steps = [55 {"action": "execute_javascript",56 "expression": "window.location.href=\"about:blank\""},57 {"action": "execute_javascript",58 "expression": "window.location.href=\"%s\"" % url},59 {"action": "wait_for_javascript",60 "expression": "performance.timing.loadEventStart", "timeout": 60*5},61 {"action": "wait", "seconds": wait_after_load_event},62 ]63 return MonsoonMeasurement.WithSteps(name, steps, repeats)64class EspnLoadPowerTest(test.Test):65 test = MonsoonMeasurement.WithSinglePageLoadSteps("loading_espn",66 url="http://m.espn.go.com")67 page_set = 'page_sets/samsung_loading_espn.json'68class WikipediaLoadPowerTest(test.Test):69 test = MonsoonMeasurement.WithSinglePageLoadSteps("loading_wikipedia",70 url="http://www.wikipedia.com")71 page_set = 'page_sets/samsung_loading_wikipedia.json'72class YoutubeLoadPowerTest(test.Test):73 test = MonsoonMeasurement.WithSinglePageLoadSteps("loading_youtube",74 url="http://m.youtube.com")75 page_set = 'page_sets/samsung_loading_youtube.json'76class OneMinutePowerTest(test.Test):77 steps = [78 {"action": "execute_javascript",79 "expression": "window.location.href=\"http://m.youtube.com\""},80 {"action": "wait", "seconds": 15 },81 {"action": "execute_javascript",82 "expression": "window.location.href=\"http://www.google.com\""},83 {"action": "wait", "seconds": 5 },84 {"action": "execute_javascript",85 "expression": "window.location.href=\"http://m.espn.go.com\""},86 {"action": "wait", "seconds": 15 },87 {"action": "execute_javascript",88 "expression": "window.location.href=\"http://www.google.com\""},89 {"action": "wait", "seconds": 5 },90 {"action": "execute_javascript",91 "expression": "window.location.href=\"http://www.wikipedia.com\""},92 {"action": "wait", "seconds": 14 },93 ]94 test = MonsoonMeasurement.WithSteps("1min", steps, repeats=5)...

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-anywherelibrary 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