如何从一个被称为功能文件中继承一个webdriver实例



我正在尝试使用Karate 0.9.0.RC4对UI自动化的新支持进行POC。

  • 有一个功能文件来验证用户所在的URL
  • 作为后台步骤,该功能文件调用另一个功能文件来实例化驱动程序并导航到网页

执行验证的功能文件如下所示:

Feature: Browser Automation
Background:
* callonce read('base-page-login.feature') { url: pageUrl, username: adminUserName, password: adminPassword}

Scenario: Login and test location
Then match driver.location == webUrlBase + '/something/topframe.jsp'

base-page-login.feature的内容如下:

Feature: Open page and login
Background:
* configure driver = { type: 'chromedriver', port: 9515, executable: '/Users/me/Code/Library/Drivers/chromedriver' }

Scenario: Login and test location
Given location url
And input //input[@name='username'] = #(username)
And input //input[@name='password'] = #(password)
When submit //input[@name='submit']

这是我的消毒日志:

21:41:48.458 [command-1541133708458] DEBUG c.intuit.karate.shell.CommandThread - command: [/Users/me/Code/Library/Drivers/chromedriver, --port=9515, --user-data-dir=/Users/me/Code/../target/1541133708457-chrome]
21:41:48.462 [main] DEBUG com.intuit.karate.web.DriverUtils - poll attempt #0 for port to be ready - localhost:9515
21:41:48.505 [command-1541133708458] DEBUG com.intuit.karate.web.WebDriver - Starting ChromeDriver 2.43.600229 (3fae4d0cda5334b4f533bede5a4787f7b832d052) on port 9515
21:41:48.506 [command-1541133708458] DEBUG com.intuit.karate.web.WebDriver - Only local connections are allowed.
21:41:48.507 [command-1541133708458] DEBUG com.intuit.karate.web.WebDriver - [1541133708.503][SEVERE]: bind() returned an error, errno=0: Address already in use (48)
21:41:48.507 [command-1541133708458] DEBUG com.intuit.karate.web.WebDriver - IPv4 port not available. Exiting...
21:41:48.508 [command-1541133708458] DEBUG c.intuit.karate.shell.CommandThread - command complete, exit code: 1 - [/Users/me/Code/Library/Drivers/chromedriver, --port=9515, --user-data-dir=/Users/me/Code/../target/1541133708457-chrome]
21:41:48.812 [main] DEBUG com.intuit.karate - request:
1 > POST http://localhost:9515/session
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Content-Length: 48
1 > Content-Type: application/json; charset=UTF-8
1 > Host: localhost:9515
1 > User-Agent: Apache-HttpClient/4.5.5 (Java/11.0.1)
{"desiredCapabilities":{"browserName":"Chrome"}}
21:41:49.890 [main] DEBUG com.intuit.karate - response time in milliseconds: 1077
1 < 200
1 < Content-Length: 903
1 < Content-Type: application/json; charset=utf-8
{"sessionId":"984ad3ef0692e6fa9f258c09c0f2bad3","status":0,"value":{"acceptInsecureCerts":false,"acceptSslCerts":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{"chromedriverVersion":"2.43.600229 (3fae4d0cda5334b4f533bede5a4787f7b832d052)","userDataDir":"/var/folders/sh/0xyv3y814d974vbvtkljdxfd603qvc/T/.org.chromium.Chromium.iROiLz"},"cssSelectorsEnabled":true,"databaseEnabled":false,"goog:chromeOptions":{"debuggerAddress":"localhost:57276"},"handlesAlerts":true,"hasTouchScreen":false,"javascriptEnabled":true,"locationContextEnabled":true,"mobileEmulationEnabled":false,"nativeEvents":true,"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platform":"Mac OS X","rotatable":false,"setWindowRect":true,"takesHeapSnapshot":true,"takesScreenshot":true,"unexpectedAlertBehaviour":"","version":"70.0.3538.67","webStorageEnabled":true}}
21:41:49.894 [main] DEBUG com.intuit.karate.web.WebDriver - init session id: 984ad3ef0692e6fa9f258c09c0f2bad3
21:41:49.904 [main] DEBUG com.intuit.karate - request:
2 > GET http://localhost:9515/session/984ad3ef0692e6fa9f258c09c0f2bad3/window
2 > Accept-Encoding: gzip,deflate
2 > Connection: Keep-Alive
2 > Host: localhost:9515
2 > User-Agent: Apache-HttpClient/4.5.5 (Java/11.0.1)
21:41:49.912 [main] DEBUG com.intuit.karate - response time in milliseconds: 8
2 < 200
2 < Content-Length: 111
2 < Content-Type: application/json; charset=utf-8
{"sessionId":"984ad3ef0692e6fa9f258c09c0f2bad3","status":0,"value":"CDwindow-A22B34D78516DB02E2D6D75AB5FB6F73"}
21:41:49.913 [main] DEBUG com.intuit.karate.web.WebDriver - init window id: CDwindow-A22B34D78516DB02E2D6D75AB5FB6F73
21:41:49.932 [main] DEBUG com.intuit.karate - request:
3 > POST http://localhost:9515/session/984ad3ef0692e6fa9f258c09c0f2bad3/url
3 > Accept-Encoding: gzip,deflate
3 > Connection: Keep-Alive
3 > Content-Length: 28
3 > Content-Type: application/json; charset=UTF-8
3 > Host: localhost:9515
3 > User-Agent: Apache-HttpClient/4.5.5 (Java/11.0.1)
{"url":"https://something.com"}
21:41:52.950 [main] DEBUG com.intuit.karate - response time in milliseconds: 3018
3 < 200
3 < Content-Length: 72
3 < Content-Type: application/json; charset=utf-8
{"sessionId":"984ad3ef0692e6fa9f258c09c0f2bad3","status":0,"value":null}
21:41:52.952 [main] DEBUG com.intuit.karate - request:
4 > DELETE http://localhost:9515/session/984ad3ef0692e6fa9f258c09c0f2bad3/window
4 > Accept-Encoding: gzip,deflate
4 > Connection: Keep-Alive
4 > Host: localhost:9515
4 > User-Agent: Apache-HttpClient/4.5.5 (Java/11.0.1)
21:41:53.058 [main] DEBUG com.intuit.karate - response time in milliseconds: 105
4 < 200
4 < Content-Length: 70
4 < Content-Type: application/json; charset=utf-8
{"sessionId":"984ad3ef0692e6fa9f258c09c0f2bad3","status":0,"value":[]}
21:41:53.060 [main] DEBUG com.intuit.karate - request:
5 > DELETE http://localhost:9515/session/984ad3ef0692e6fa9f258c09c0f2bad3
5 > Accept-Encoding: gzip,deflate
5 > Connection: Keep-Alive
5 > Host: localhost:9515
5 > User-Agent: Apache-HttpClient/4.5.5 (Java/11.0.1)
21:41:53.079 [main] DEBUG com.intuit.karate - response time in milliseconds: 19
5 < 200
5 < Content-Length: 40
5 < Content-Type: application/json; charset=utf-8
{"sessionId":"","status":0,"value":null}
21:41:53.080 [main] DEBUG c.intuit.karate.shell.CommandThread - closing command: 1541133708458
Exception in thread "main" java.lang.NullPointerException
at com.intuit.karate.IdeUtils.details(IdeUtils.java:143)
at com.intuit.karate.IdeUtils.log(IdeUtils.java:165)
at com.intuit.karate.IdeUtils.exec(IdeUtils.java:66)
at cucumber.api.cli.Main.main(Main.java:36)
Process finished with exit code 1

如果我直接管理Selenium,我的期望是使用相同的Selenium实例。我看到的行为是,当代码到达返回"验证"功能文件的点时,驱动程序关闭。由于这是一个RC,这可能是一个已知的问题,也可能是我的错误,但我想从社区获得意见。

你是对的,这需要解决。到目前为止,webdriver实例在任何场景后都会自动关闭,这里的解决方案是在a(它是一个被称为场景,b(共享范围正在使用的情况下不要这样做

我会把这一点结合起来,我们应该在一周内发布RC5。