How to use js_type method in SeleniumBase

Best Python code snippet using SeleniumBase

js_externs_generator.py

Source:js_externs_generator.py Github

copy

Full Screen

1# Copyright 2015 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.4"""5Generator that produces an externs file for the Closure Compiler.6Note: This is a work in progress, and generated externs may require tweaking.7See https://developers.google.com/closure/compiler/docs/api-tutorial3#externs8"""9from code import Code10from model import *11from schema_util import *12import os13from datetime import datetime14import re15LICENSE = ("""// Copyright %s The Chromium Authors. All rights reserved.16// Use of this source code is governed by a BSD-style license that can be17// found in the LICENSE file.18""" % datetime.now().year)19class JsExternsGenerator(object):20 def Generate(self, namespace):21 return _Generator(namespace).Generate()22class _Generator(object):23 def __init__(self, namespace):24 self._namespace = namespace25 def Generate(self):26 """Generates a Code object with the schema for the entire namespace.27 """28 c = Code()29 (c.Append(LICENSE)30 .Append()31 .Append('/** @fileoverview Externs generated from namespace: %s */' %32 self._namespace.name)33 .Append())34 c.Cblock(self._GenerateNamespaceObject())35 for js_type in self._namespace.types.values():36 c.Cblock(self._GenerateType(js_type))37 for function in self._namespace.functions.values():38 c.Cblock(self._GenerateFunction(function))39 for event in self._namespace.events.values():40 c.Cblock(self._GenerateEvent(event))41 return c42 def _GenerateType(self, js_type):43 """Given a Type object, returns the Code for this type's definition.44 """45 c = Code()46 if js_type.property_type is PropertyType.ENUM:47 c.Concat(self._GenerateEnumJsDoc(js_type))48 else:49 c.Concat(self._GenerateTypeJsDoc(js_type))50 return c51 def _GenerateEnumJsDoc(self, js_type):52 """ Given an Enum Type object, returns the Code for the enum's definition.53 """54 c = Code()55 (c.Sblock(line='/**', line_prefix=' * ')56 .Append('@enum {string}')57 .Append(self._GenerateSeeLink('type', js_type.simple_name))58 .Eblock(' */'))59 c.Append('chrome.%s.%s = {' % (self._namespace.name, js_type.name))60 def get_property_name(e):61 # Enum properties are normified to be in ALL_CAPS_STYLE.62 # Assume enum '1ring-rulesThemAll'.63 # Transform to '1ring-rules_Them_All'.64 e = re.sub(r'([a-z])([A-Z])', r'\1_\2', e)65 # Transform to '1ring_rules_Them_All'.66 e = re.sub(r'\W', '_', e)67 # Transform to '_1ring_rules_Them_All'.68 e = re.sub(r'^(\d)', r'_\1', e)69 # Transform to '_1RING_RULES_THEM_ALL'.70 return e.upper()71 c.Append('\n'.join(72 [" %s: '%s'," % (get_property_name(v.name), v.name)73 for v in js_type.enum_values]))74 c.Append('};')75 return c76 def _IsTypeConstructor(self, js_type):77 """Returns true if the given type should be a @constructor. If this returns78 false, the type is a typedef.79 """80 return any(prop.type_.property_type is PropertyType.FUNCTION81 for prop in js_type.properties.values())82 def _GenerateTypeJsDoc(self, js_type):83 """Generates the documentation for a type as a Code.84 Returns an empty code object if the object has no documentation.85 """86 c = Code()87 c.Sblock(line='/**', line_prefix=' * ')88 if js_type.description:89 for line in js_type.description.splitlines():90 c.Append(line)91 is_constructor = self._IsTypeConstructor(js_type)92 if is_constructor:93 c.Comment('@constructor', comment_prefix = ' * ', wrap_indent=4)94 else:95 c.Concat(self._GenerateTypedef(js_type.properties))96 c.Append(self._GenerateSeeLink('type', js_type.simple_name))97 c.Eblock(' */')98 var = 'var ' + js_type.simple_name99 if is_constructor: var += ' = function() {}'100 var += ';'101 c.Append(var)102 return c103 def _GenerateTypedef(self, properties):104 """Given an OrderedDict of properties, returns a Code containing a @typedef.105 """106 if not properties: return Code()107 c = Code()108 c.Append('@typedef {')109 c.Concat(self._GenerateObjectDefinition(properties), new_line=False)110 c.Append('}', new_line=False)111 return c112 def _GenerateObjectDefinition(self, properties):113 """Given an OrderedDict of properties, returns a Code containing the114 description of an object.115 """116 if not properties: return Code()117 c = Code()118 c.Sblock('{')119 first = True120 for field, prop in properties.items():121 # Avoid trailing comma.122 # TODO(devlin): This will be unneeded, if/when123 # https://github.com/google/closure-compiler/issues/796 is fixed.124 if not first:125 c.Append(',', new_line=False)126 first = False127 js_type = self._TypeToJsType(prop.type_)128 if prop.optional:129 js_type = (Code().130 Append('(').131 Concat(js_type, new_line=False).132 Append('|undefined)', new_line=False))133 c.Append('%s: ' % field, strip_right=False)134 c.Concat(js_type, new_line=False)135 c.Eblock('}')136 return c137 def _GenerateFunctionJsDoc(self, function):138 """Generates the documentation for a function as a Code.139 Returns an empty code object if the object has no documentation.140 """141 c = Code()142 c.Sblock(line='/**', line_prefix=' * ')143 if function.description:144 c.Comment(function.description, comment_prefix='')145 def append_field(c, tag, js_type, name, optional, description):146 c.Append('@%s {' % tag)147 c.Concat(js_type, new_line=False)148 if optional:149 c.Append('=', new_line=False)150 c.Append('} %s' % name, new_line=False)151 if description:152 c.Comment(' %s' % description, comment_prefix='',153 wrap_indent=4, new_line=False)154 for param in function.params:155 append_field(c, 'param', self._TypeToJsType(param.type_), param.name,156 param.optional, param.description)157 if function.callback:158 append_field(c, 'param', self._FunctionToJsFunction(function.callback),159 function.callback.name, function.callback.optional,160 function.callback.description)161 if function.returns:162 append_field(c, 'return', self._TypeToJsType(function.returns),163 '', False, function.returns.description)164 if function.deprecated:165 c.Append('@deprecated %s' % function.deprecated)166 c.Append(self._GenerateSeeLink('method', function.name))167 c.Eblock(' */')168 return c169 def _FunctionToJsFunction(self, function):170 """Converts a model.Function to a JS type (i.e., function([params])...)"""171 c = Code()172 c.Append('function(')173 for i, param in enumerate(function.params):174 c.Concat(self._TypeToJsType(param.type_), new_line=False)175 if i is not len(function.params) - 1:176 c.Append(', ', new_line=False, strip_right=False)177 c.Append('):', new_line=False)178 if function.returns:179 c.Concat(self._TypeToJsType(function.returns), new_line=False)180 else:181 c.Append('void', new_line=False)182 return c183 def _TypeToJsType(self, js_type):184 """Converts a model.Type to a JS type (number, Array, etc.)"""185 if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE):186 return Code().Append('number')187 if js_type.property_type is PropertyType.OBJECT:188 if js_type.properties:189 return self._GenerateObjectDefinition(js_type.properties)190 return Code().Append('Object')191 if js_type.property_type is PropertyType.ARRAY:192 return (Code().Append('!Array<').193 Concat(self._TypeToJsType(js_type.item_type), new_line=False).194 Append('>', new_line=False))195 if js_type.property_type is PropertyType.REF:196 ref_type = js_type.ref_type197 # Enums are defined as chrome.fooAPI.MyEnum, but types are defined simply198 # as MyType.199 if self._namespace.types[ref_type].property_type is PropertyType.ENUM:200 ref_type = '!chrome.%s.%s' % (self._namespace.name, ref_type)201 return Code().Append(ref_type)202 if js_type.property_type is PropertyType.CHOICES:203 c = Code()204 c.Append('(')205 for i, choice in enumerate(js_type.choices):206 c.Concat(self._TypeToJsType(choice), new_line=False)207 if i is not len(js_type.choices) - 1:208 c.Append('|', new_line=False)209 c.Append(')', new_line=False)210 return c211 if js_type.property_type is PropertyType.FUNCTION:212 return self._FunctionToJsFunction(js_type.function)213 if js_type.property_type is PropertyType.ANY:214 return Code().Append('*')215 if js_type.property_type.is_fundamental:216 return Code().Append(js_type.property_type.name)217 return Code().Append('?') # TODO(tbreisacher): Make this more specific.218 def _GenerateFunction(self, function):219 """Generates the code representing a function, including its documentation.220 For example:221 /**222 * @param {string} title The new title.223 */224 chrome.window.setTitle = function(title) {};225 """226 c = Code()227 params = self._GenerateFunctionParams(function)228 (c.Concat(self._GenerateFunctionJsDoc(function))229 .Append('chrome.%s.%s = function(%s) {};' % (self._namespace.name,230 function.name,231 params))232 )233 return c234 def _GenerateEvent(self, event):235 """Generates the code representing an event.236 For example:237 /** @type {!ChromeEvent} */238 chrome.bookmarks.onChildrenReordered;239 """240 c = Code()241 c.Sblock(line='/**', line_prefix=' * ')242 if (event.description):243 c.Comment(event.description, comment_prefix='')244 c.Append('@type {!ChromeEvent}')245 c.Append(self._GenerateSeeLink('event', event.name))246 c.Eblock(' */')247 c.Append('chrome.%s.%s;' % (self._namespace.name, event.name))248 return c249 def _GenerateNamespaceObject(self):250 """Generates the code creating namespace object.251 For example:252 /**253 * @const254 */255 chrome.bookmarks = {};256 """257 c = Code()258 (c.Append("""/**259 * @const260 */""")261 .Append('chrome.%s = {};' % self._namespace.name))262 return c263 def _GenerateFunctionParams(self, function):264 params = function.params[:]265 if function.callback:266 params.append(function.callback)267 return ', '.join(param.name for param in params)268 def _GenerateSeeLink(self, object_type, object_name):269 """Generates a @see link for a given API 'object' (type, method, or event).270 """271 # NOTE(devlin): This is kind of a hack. Some APIs will be hosted on272 # developer.chrome.com/apps/ instead of /extensions/, and some APIs have273 # '.'s in them (like app.window), which should resolve to 'app_window'.274 # Luckily, the doc server has excellent url resolution, and knows exactly275 # what we mean. This saves us from needing any complicated logic here.276 return ('@see https://developer.chrome.com/extensions/%s#%s-%s' %...

Full Screen

Full Screen

js_util.py

Source:js_util.py Github

copy

Full Screen

1# Copyright 2015 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.4from code import Code5from model import PropertyType6from datetime import datetime7LICENSE = """// Copyright %s The Chromium Authors. All rights reserved.8// Use of this source code is governed by a BSD-style license that can be9// found in the LICENSE file.10"""11INFO = """// This file was generated by:12// %s.13"""14class JsUtil(object):15 """A helper class for generating JS Code.16 """17 def GetLicense(self):18 """Returns the license text for JS extern and interface files.19 """20 return (LICENSE % datetime.now().year)21 def GetInfo(self, tool):22 """Returns text describing how the file was generated.23 """24 return (INFO % tool)25 def GenerateObjectDefinition(self, namespace_name, properties):26 """Given an OrderedDict of properties, returns a Code containing the27 description of an object.28 """29 if not properties: return Code()30 c = Code()31 c.Sblock('{')32 first = True33 for field, prop in properties.items():34 # Avoid trailing comma.35 # TODO(devlin): This will be unneeded, if/when36 # https://github.com/google/closure-compiler/issues/796 is fixed.37 if not first:38 c.Append(',', new_line=False)39 first = False40 js_type = self._TypeToJsType(namespace_name, prop.type_)41 if prop.optional:42 js_type = (Code().43 Append('(').44 Concat(js_type, new_line=False).45 Append('|undefined)', new_line=False))46 c.Append('%s: ' % field, strip_right=False)47 c.Concat(js_type, new_line=False)48 c.Eblock('}')49 return c50 def GenerateFunctionJsDoc(self, namespace_name, function):51 """Generates the documentation for a function as a Code.52 Returns an empty code object if the object has no documentation.53 """54 c = Code()55 c.Sblock(line='/**', line_prefix=' * ')56 if function.description:57 c.Comment(function.description, comment_prefix='')58 def append_field(c, tag, js_type, name, optional, description):59 c.Append('@%s {' % tag)60 c.Concat(js_type, new_line=False)61 if optional:62 c.Append('=', new_line=False)63 c.Append('} %s' % name, new_line=False)64 if description:65 c.Comment(' %s' % description, comment_prefix='',66 wrap_indent=4, new_line=False)67 for param in function.params:68 append_field(c, 'param', self._TypeToJsType(namespace_name, param.type_),69 param.name, param.optional, param.description)70 if function.callback:71 append_field(c, 'param',72 self._FunctionToJsFunction(namespace_name,73 function.callback),74 function.callback.name, function.callback.optional,75 function.callback.description)76 if function.returns:77 append_field(c, 'return',78 self._TypeToJsType(namespace_name, function.returns),79 '', False, function.returns.description)80 if function.deprecated:81 c.Append('@deprecated %s' % function.deprecated)82 c.Append(self.GenerateSeeLink(namespace_name, 'method', function.name))83 c.Eblock(' */')84 return c85 def _FunctionToJsFunction(self, namespace_name, function):86 """Converts a model.Function to a JS type (i.e., function([params])...)"""87 c = Code()88 c.Append('function(')89 for i, param in enumerate(function.params):90 c.Concat(self._TypeToJsType(namespace_name, param.type_), new_line=False)91 if i is not len(function.params) - 1:92 c.Append(', ', new_line=False, strip_right=False)93 c.Append('):', new_line=False)94 if function.returns:95 c.Concat(self._TypeToJsType(namespace_name, function.returns),96 new_line=False)97 else:98 c.Append('void', new_line=False)99 return c100 def _TypeToJsType(self, namespace_name, js_type):101 """Converts a model.Type to a JS type (number, Array, etc.)"""102 if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE):103 return Code().Append('number')104 if js_type.property_type is PropertyType.OBJECT:105 if js_type.properties:106 return self.GenerateObjectDefinition(namespace_name,107 js_type.properties)108 return Code().Append('Object')109 if js_type.property_type is PropertyType.ARRAY:110 return (Code().Append('!Array<').111 Concat(self._TypeToJsType(namespace_name, js_type.item_type),112 new_line=False).113 Append('>', new_line=False))114 if js_type.property_type is PropertyType.REF:115 ref_type = '!chrome.%s.%s' % (namespace_name, js_type.ref_type)116 return Code().Append(ref_type)117 if js_type.property_type is PropertyType.CHOICES:118 c = Code()119 c.Append('(')120 for i, choice in enumerate(js_type.choices):121 c.Concat(self._TypeToJsType(namespace_name, choice), new_line=False)122 if i is not len(js_type.choices) - 1:123 c.Append('|', new_line=False)124 c.Append(')', new_line=False)125 return c126 if js_type.property_type is PropertyType.FUNCTION:127 return self._FunctionToJsFunction(namespace_name, js_type.function)128 if js_type.property_type is PropertyType.ANY:129 return Code().Append('*')130 if js_type.property_type.is_fundamental:131 return Code().Append(js_type.property_type.name)132 return Code().Append('?') # TODO(tbreisacher): Make this more specific.133 def GenerateSeeLink(self, namespace_name, object_type, object_name):134 """Generates a @see link for a given API 'object' (type, method, or event).135 """136 # NOTE(devlin): This is kind of a hack. Some APIs will be hosted on137 # developer.chrome.com/apps/ instead of /extensions/, and some APIs have138 # '.'s in them (like app.window), which should resolve to 'app_window'.139 # Luckily, the doc server has excellent url resolution, and knows exactly140 # what we mean. This saves us from needing any complicated logic here.141 return ('@see https://developer.chrome.com/extensions/%s#%s-%s' %...

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