Coverage for selenible/modules/content.py: 15%
36 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-05 14:24 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-05 14:24 +0000
1import re
3import yaml
5update_style_schema = yaml.safe_load("""
6allOf:
7 - type: object
8 - "$ref": "#/definitions/common/locator"
9""")
12def Base_update_style(self, param):
13 """
14 - name: red background
15 update_style:
16 id: element1
17 "background-color": red
18 - name: dismiss heading
19 update_style:
20 tag: h1
21 display: none
22 """
23 newstyle = self.removelocator(param)
24 for elem in self.findmany(param):
25 self.log.debug("update style %s <- %s", elem.id, newstyle)
26 for k, v in newstyle.items():
27 script = "".join(
28 [
29 "arguments[0].style[",
30 repr(k),
31 "]=",
32 repr(v),
33 ";",
34 "return arguments[0];",
35 ]
36 )
37 self.execute(script, elem)
40update_content_schema = yaml.safe_load("""
41allOf:
42 - type: object
43 properties:
44 pattern: {type: string}
45 replacement: {type: string}
46 regexp: {type: boolean}
47 flag:
48 type: string
49 enum: [i, g]
50 - "$ref": "#/definitions/common/locator"
51""")
54def Base_update_content(self, param):
55 """
56 - name: mask some number
57 update_content:
58 pattern: "[0-9]"
59 replacement: "*"
60 regexp: true
61 id: element1
62 """
63 pattern = param.get("pattern")
64 replacement = param.get("replacement")
65 regexp = param.get("regexp", False)
66 flag = param.get("flag", "g")
67 if pattern is None or replacement is None:
68 raise Exception(f"invalid parameter: {param}")
69 if regexp:
70 # check regexp
71 re.compile(pattern)
72 # OK
73 script = "".join(
74 [
75 "arguments[0].innerHTML=arguments[0].innerHTML.replace(/",
76 pattern,
77 "/",
78 flag,
79 ",",
80 repr(replacement),
81 (");return arguments[0];"),
82 ]
83 )
84 else:
85 script = "".join(
86 [
87 "arguments[0].innerHTML=arguments[0].innerHTML.replace(",
88 repr(pattern),
89 ",",
90 repr(replacement),
91 (");return arguments[0];"),
92 ]
93 )
94 for elem in self.findmany(param):
95 self.execute(script, elem)
98update_attribute_schema = yaml.safe_load("""
99allOf:
100 - "$ref": "#/definitions/common/locator"
101 - type: object
102 properties:
103 replacement: {type: object}
104""")
107def Base_update_attribute(self, param):
108 """
109 - name: link redirection
110 update_attribute:
111 tag: a
112 href: http://example.com/
113 - name: rename
114 update_attribute:
115 id: element1
116 replacement:
117 id: new-element1
118 class: null # remove attribute
119 """
120 newattr = self.removelocator(param)
121 if len(newattr) == 0:
122 newattr = param.get("replacement", {})
123 for elem in self.findmany(param):
124 self.log.debug("update style %s <- %s", elem.id, newattr)
125 for k, v in newattr.items():
126 if v is None:
127 script = "".join(
128 [
129 "arguments[0].removeAttribute(",
130 repr(k),
131 ");",
132 "return arguments[0];",
133 ]
134 )
135 else:
136 script = "".join(
137 [
138 "arguments[0].setAttribute(",
139 repr(k),
140 ",",
141 repr(v),
142 ");",
143 "return arguments[0];",
144 ]
145 )
146 self.execute(script, elem)