Scenario: [1:11]
ms: 12593
>>
Background:
4
* def session = { capabilities: { alwaysMatch: { browserName: 'chrome' } } }
16
08:36:40.491 using driver: chromedriver
5
* configure driver = { type:'chromedriver',port: 9222, executable: '/Users/palakshah/Downloads/chromedriver-mac-arm64/chromedriver', start: true, webDriverSession: '#(session)' }
2
6
* driver serverUrl + '/00'
2865
7
* def dimensions = karate.get('dimensions')
16
8
* if (dimensions) driver.dimensions = dimensions
3
9
* def skipSlowTests = karate.properties['skip.slow.tests']
2
# driver.send() (has to be first)
# * if (driverType == 'chrome') karate.call('12.feature')
# driver.url | driver.title | waitForUrl() | refresh() | back() | forward() | driver.dimensions
# * call read('01.feature')
# waitFor() | waitForText() | waitForEnabled()
19
* call read('02.feature')
3109
>>
src.test.java.driver.02
3082
4
* driver serverUrl + '/02'
31
# wait for slow loading element
8
* waitFor('#slowDiv')
3034
08:36:43.475 waitUntil (js) - retry #1
# this is a string "contains" match for convenience
11
* waitForText('#slowDiv', 'APPEARED')
8
# how to search the whole html
14
* waitForText('body', 'APPEARED')
5
# will be false if disabled
17
* waitForEnabled('#slowDiv')
5
# script() | waitUntil()
22
* call read('03.feature')
3121
>>
src.test.java.driver.03
3107
4
* driver serverUrl + '/03'
21
# different ways to use waitUntil()
8
* waitUntil('#helloDiv', "function(e){ return e.innerHTML == 'hello world' }")
3023
08:36:46.551 waitUntil (js) - retry #1
9
* waitUntil('#helloDiv', "_.innerHTML == 'hello world'")
6
10
* waitUntil('#helloDiv', '!_.disabled')
4
# different ways to use script()
13
* match script('#helloDiv', "function(e){ return e.innerHTML }") == 'hello world'
14
14
* match script('#helloDiv', '_.innerHTML') == 'hello world'
4
15
* match script('#helloDiv', '!_.disabled') == true
4
16
* match script('#helloDiv', "_.style['color']") == 'red'
6
17
* match script('.styled-div', "function(e){ return getComputedStyle(e)['font-size'] }") == '30px'
5
18
* match script('.styled-div', "_ => getComputedStyle(_)['font-size']") == '30px'
5
# Regression test for https://github.com/karatelabs/karate/issues/1786
# Tests that Karate can handle console logging -- we can't assert it is working, but we can check that it doesn't crash devtools connection
# Expect Karate to log "[console] testing"
23
* script("console.log('testing')")
6
# Expect Karate to log "[console] true"
26
* script("console.log(true)")
4
# Expect Karate to log "[console] 1"
29
* script("console.log(1)")
4
# cookies
25
* if (driverType != 'safaridriver' && driverType != 'playwright') karate.call('04.feature')
132
>>
src.test.java.driver.04
119
4
* driver serverUrl + '/04'
30
# foo=bar is set by the server
8
* def cookie1 = { name: 'foo', value: 'bar' }
0
9
And match driver.cookies contains deep cookie1
9
10
And match cookie('foo') contains deep cookie1
5
12
* def cookie2 = { name: 'hello', value: 'world' }
0
13
* cookie(cookie2)
10
14
* match driver.cookies contains deep cookie2
6
# delete cookie
17
* deleteCookie('foo')
24
18
* match driver.cookies !contains '#(^cookie1)'
7
# clear cookies
21
* clearCookies()
8
22
* match driver.cookies == '#[0]'
7
# set multiple cookies at once e.g. from an API call
25
* def data = [{ name: 'one', value: '1' }, { name: 'two', value: '2' }]
0
26
* driver.cookies = data
9
27
* match driver.cookies contains deep data
4
# driver.intercept
28
* if (driverType == 'chrome' || driverType == 'playwright') karate.call('05.feature')
1
# position()
31
* call read('06.feature')
49
>>
src.test.java.driver.06
38
4
* driver serverUrl + '/06'
23
7
* def loc1 = position('#first')
9
8
* match loc1 contains { x: '#number', y: '#number', width: '#number', height: '#number' }
0
9
* def loc2 = position('#second')
5
10
* match loc1.y == loc2.y
1
11
* match loc1.width == 102
0
12
* match loc1.width == loc2.width
0
# input() | value() | text() | html()
34
* call read('07.feature')
278
>>
src.test.java.driver.07
267
4
* driver serverUrl + '/07'
33
7
* input('#inputId', 'hello world')
146
8
* click('input[name=submitName]')
10
9
* match value('#inputId') == 'hello world'
5
10
* match text('#valueId') == 'hello world'
4
11
* match html('#valueId') == '<div id="valueId">hello world</div>'
6
12
* def expected = '72d72u69d69u76d76u76d76u79d79u32d32u87d87u79d79u82d82u76d76u68d68u'
0
13
* if (driverType == 'chrome') expected = '104d104u101d101u108d108u108d108u111d111u32d32u119d119u111d111u114d114u108d108u100d100u'
0
14
* if (driverType == 'safaridriver') expected = '72d72u69d69u76d76u76d76u79d79u65d65u87d87u79d79u82d82u76d76u68d68u'
0
15
* match text('#pressedId') == expected
5
17
* clear('#inputId')
5
18
* waitFor('#inputId').input('hello world')
30
19
* waitFor('input[name=submitName]').click()
11
20
* match value('#inputId') == 'hello world'
5
21
* match text('#valueId') == 'hello world'
3
22
* match html('#valueId') == '<div id="valueId">hello world</div>'
4
# switchFrame()
37
* call read('08.feature')
499
>>
src.test.java.driver.08
479
4
* driver serverUrl + '/08'
54
7
* match text('#messageId') == 'this div is outside the iframe'
6
8
* switchFrame('#frameId')
25
9
* input('#inputId', 'hello world')
26
10
* click('input[name=submitName]')
4
11
* match value('#inputId') == 'hello world'
5
12
* match text('#valueId') == 'hello world'
4
# switch back to parent frame
15
* switchFrame(-1)
4
16
* match text('#messageId') == 'this div is outside the iframe'
4
4
* driver serverUrl + '/08'
35
19
* match text('#messageId') == 'this div is outside the iframe'
4
20
* switchFrame(0)
8
21
* input('#inputId', 'hello world')
63
22
* click('input[name=submitName]')
26
23
* match value('#inputId') == 'hello world'
4
24
* match text('#valueId') == 'hello world'
5
# switch back to parent frame
27
* switchFrame(-1)
2
28
* match text('#messageId') == 'this div is outside the iframe'
3
4
* driver serverUrl + '/08'
28
#* if (driverType != 'chrome') karate.abort()
32
* switchFrame('#uploadFrameId')
33
33
* driver.inputFile('#fileToUpload', '08.pdf')
33
34
* click('#uploadButton')
99
35
* waitForText('#uploadMessage', '08.pdf')
4
# wildcard locators
40
* call read('09.feature')
78
>>
src.test.java.driver.09
72
4
* driver serverUrl + '/09'
24
7
* switchFrame(-1)
7
8
* def parent = waitFor('.div-02')
5
# make sure the find-by-text works relative to search node and not from document root
10
* def found = parent.locate('{}Some Text')
11
11
* match found.attribute('class') == 'div-04'
8
# find all, exact match
14
* def list1 = locateAll('{}Some Text')
5
15
* assert list1.length == 2
1
# find all, contains match
18
* def list2 = locateAll('{^}Text')
5
19
* assert list2.length == 2
0
# works with exists too
22
* match exists('{}Some Text') == true
6
# element position
43
* call read('10.feature')
98
>>
src.test.java.driver.10
92
4
* driver serverUrl + '/10'
21
7
* waitFor('{}this test verifies an element can be located even when the page needs scrolling')
6
8
* scroll('{}Label without scroll :').input('it works')
30
# TODO rightOf() not working only on firefox (is apple m1 the reason ?)
11
* if (driverType == 'geckodriver') karate.abort()
1
12
* scroll('{}Label with scroll :').rightOf().input('it should not fail')
35
# switchPage()
46
* if ((driverType == 'chrome' || driverType == 'playwright') && !skipSlowTests) karate.call('11.feature')
0
# switchPage() with external URLs
49
* if (driverType == 'playwright' && !skipSlowTests) karate.call('13.feature')
0
# survive Target.detachedFromTarget with nested iframes
52
* if ((driverType == 'chrome' || driverType == 'playwright') && !skipSlowTests) karate.call('14.feature')
0
# xpath locators
55
* call read('15.feature')
109
>>
src.test.java.driver.15
103
4
* driver serverUrl + '/15'
19
# xpath script in whole document
8
* def first = script('//li', '_.textContent')
4
9
* match first == 'item 1'
0
# xpath scriptAll in whole document
12
* def list = scriptAll('//li', '_.textContent')
6
13
* match list == ['item 1', 'item 2', 'item 2.1', 'item 2.2', 'item 3']
0
# get first ul in entire document
16
* def first = locate('//ul')
5
# validates that operations other than locate also work...
18
* match attribute('//ul', 'id') == 'first'
5
# relative xpath - locate
21
* def second = first.locate('./ul/li')
8
22
* match second.text == 'item 2.1'
4
# relative xpath - locate all
25
* def seconds = second.locateAll('..//li')
8
26
* assert seconds.length == 2
0
27
* match (seconds[1].text) == 'item 2.2'
4
# relative xpath - script all
30
* match first.locateAll('./li').length == 3
9
31
* def level1 = first.scriptAll('./li', '_.textContent')
7
32
* match level1 == ['item 1', 'item 2', 'item 3']
0
# relative xpath (any depth) script all
35
* def all = first.scriptAll('.//li', '_.textContent')
7
36
* match all == ['item 1', 'item 2', 'item 2.1', 'item 2.2', 'item 3']
0
38
* def children = first.children
4
39
* match (children.length) == 4
0
40
* match (children[0].text) == 'item 1'
3
41
* match (children[0].parent).attribute('id') == 'first'
8
# image comparison
58
* if (!skipSlowTests) karate.call('16.feature')
1714
>>
src.test.java.driver.16
1706
4
* driver serverUrl + '/16'
22
# compare screenshot (normally would read a baseline from disk but we'll simulate here)
8
* def baselineBytes = screenshot()
137
9
* compareImage { baseline: '#(baselineBytes)', latest: '#(baselineBytes)' }
359
# compare mismatched screenshot with ignoredBoxes
12
* click('#show')
6
13
* def latestBytes = screenshot()
115
14
* def ignoredBoxes =
0
[{
top: 0,
bottom: 200,
left: 0,
right: 200
}]
23
* compareImage { baseline: '#(baselineBytes)', latest: '#(baselineBytes)', options: { ignoredBoxes: '#(ignoredBoxes)' } }
257
# compare mismatched screenshot: allowed to fail with custom failureThreshold
26
* compareImage { baseline: '#(baselineBytes)', latest: '#(latestBytes)', options: { failureThreshold: 99.9 } }
279
# compare mismatched screenshot: allowed to fail with mismatchShouldPass
29
* configure imageComparison = { mismatchShouldPass: true }
0
30
* def result = karate.compareImage(baselineBytes, latestBytes)
307
08:36:52.361 image comparison failed: latest image differed from baseline more than allowable threshold: 5.282620179609086% >= 0.0%
31
* match result.isMismatch == true
1
# missing baseline screenshot: allowed to fail with mismatchShouldPass
34
* def result = karate.compareImage(null, latestBytes)
222
08:36:52.587 image comparison failed: baseline image was empty or not found
35
* match result.isBaselineMissing == true
1
# switch to root session on page close
61
* call read('17.feature')
352
>>
src.test.java.driver.17
320
4
* driver serverUrl + '/17_a'
46
7
* def openPageInNewTab = (pageUrl) => driver.script(`window.open('${pageUrl}', '_blank', 'noopener')`)
3
9
* waitFor('input[name=a]').input('a1')
49
11
* openPageInNewTab(serverUrl + '/17_b')
37
12
* switchPage('17_b')
49
13
* waitFor('input[name=b]').input('b1')
42
14
* close()
60
16
* switchPage('17_a')
13
17
* input('input[name=a]', 'a2')
19
# submit and retry Should in theory pass for all browsers but playwright handles retries a bit different so expectations that work with it may not work with other implementations.
64
* if (driverType == 'playwright') karate.call('18.feature')
0
# friendly locators
67
* if (driverType != 'geckodriver') karate.call('19.feature')
150
>>
src.test.java.driver.19
143
4
* driver serverUrl + '/19'
29
5
* timeout(2000)
2
9
* match locate('{^i}Center').rightOf().find('span').attribute('id') == 'e'
21
10
* match rightOf('{^i}Center').find('span').attribute('id') == 'e'
23
11
* match leftOf('{^i}Center').find('span').attribute('id') == 'w'
19
12
* match above('{^i}Center').find('span').attribute('id') == 'n'
16
13
* match below('{^i}Center').find('span').attribute('id') == 's'
12
14
* match leftOf('{^i}Center').find('{^b}West').attribute('id') == 'w_nested'
12
# PW returns them all sorted by distance. Currently, the Karate API only allows find() to return one element.
16
* match near('{^i}Center').find('span').attribute('id') == 'n'
10
17
* if (driverType != 'playwright') karate.abort()
0
08:36:53.095 abort at src/test/java/driver/19.feature:17
# Drivers other than playwright will fail the tests below because:
# they will fail calling rightOf('{^i}Center').find('{^b}West') rather than return an element with present = false
# they don't support xpath in find()
21
* match rightOf('{^i}Center').find('{^b}West').present == false
0
22
* match locate('//i').rightOf().find('//span').attribute('id') == 'e'
0
23
* match locate('{^i}Center').rightOf().find('//span').attribute('id') == 'e'
0