问题
Is there a way to set a custom useragent for just one test in nightwatch?
I want to test a page using both mobile and non-mobile useragents to ensure the proper experience is shown.
I tried setting it as a cookie but it didn't work:
browser
.setCookie({
name : "User_Agent",
value : "iphone",
})
.url('...')
// etc
回答1:
Your test suite must be independent of the browser targeted.
You should set the user agent directly in the NightWatch configuration json file :
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"port" : 4444
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : true,
"path" : "screenshots"
},
"desiredCapabilities": {
"browserName": "chrome",
"chromeOptions": {
"args": [
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A",
"--window-size=320,640"
]
},
"javascriptEnabled": true,
"acceptSslCerts": true
}
}
If necessary, you can find a major list of user agents here : http://www.useragentstring.com/pages/Safari/
来源:https://stackoverflow.com/questions/29522757/custom-useragent-in-nightwatch