Skip to content

Commit 59a68ac

Browse files
committed
Test escape_xpath_value
1 parent 3ef5bee commit 59a68ac

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

src/SeleniumLibrary/utils/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
def escape_xpath_value(value):
2424
if '"' in value and "'" in value:
2525
parts_wo_apos = value.split("'")
26-
return "concat('%s')" % "', \"'\", '".join(parts_wo_apos)
26+
escaped = "', \"'\", '".join(parts_wo_apos)
27+
return f"concat('{escaped}')"
2728
if "'" in value:
28-
return '"%s"' % value
29-
return "'%s'" % value
29+
return f'"{value}"'
30+
return f"'{value}'"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
3+
import pytest
4+
from approvaltests.approvals import verify_all
5+
from approvaltests.reporters.generic_diff_reporter_factory import (
6+
GenericDiffReporterFactory,
7+
)
8+
from robot.utils import WINDOWS
9+
10+
from SeleniumLibrary.utils import escape_xpath_value
11+
12+
13+
@pytest.fixture(scope="module")
14+
def reporter():
15+
path = os.path.dirname(__file__)
16+
reporter_json = os.path.abspath(
17+
os.path.join(path, "..", "approvals_reporters.json")
18+
)
19+
factory = GenericDiffReporterFactory()
20+
factory.load(reporter_json)
21+
return factory.get_first_working()
22+
23+
24+
@pytest.mark.skipif(WINDOWS, reason="ApprovalTest do not support different line feeds")
25+
def test_string(reporter):
26+
results = []
27+
results.append(escape_xpath_value("tidii"))
28+
results.append(escape_xpath_value('"tidii"'))
29+
results.append(escape_xpath_value("'tidii'"))
30+
results.append(escape_xpath_value("\"'tidii'"))
31+
results.append(escape_xpath_value("\"'tidii'\""))
32+
results.append(escape_xpath_value("\"'t\"id\"ii'\""))
33+
results.append(escape_xpath_value('"\'tidii\'"'))
34+
results.append(escape_xpath_value('"\'ti\'d\'ii\'"'))
35+
verify_all("Escape xpath value", results, reporter=reporter)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Escape xpath value
2+
3+
0) 'tidii'
4+
1) '"tidii"'
5+
2) "'tidii'"
6+
3) concat('"', "'", 'tidii', "'", '')
7+
4) concat('"', "'", 'tidii', "'", '"')
8+
5) concat('"', "'", 't"id"ii', "'", '"')
9+
6) concat('"', "'", 'tidii', "'", '"')
10+
7) concat('"', "'", 'ti', "'", 'd', "'", 'ii', "'", '"')

0 commit comments

Comments
 (0)