英文:
Pass test case name on name capability (session) of browserstack, using geb-spock
问题
I am using geb-spock
I have tried with Junit but found out that TestName does not @Before
code of GebConf.groovy file
caps.setCapability("name","");
I did not want to hard code the name
Note: If I leave name blank like about then session id shows as name
environments {
browserstackchrome {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "80.0");
caps.setCapability("resolution", "1366x768");
caps.setCapability("project", "KPNA_Geb");
caps.setCapability("build", "build 1");
caps.setCapability("name","");
println(login3.testloginname) // Print Null value
//getClass().getEnclosingMethod().getName()
driver = {
new RemoteWebDriver(new URL("http://" + username + ":" + accessKey + "@" + server + "/wd/hub"), caps)
};
}
Code of test case
class login3 extends GebReportingSpec{
public static String testloginname ;
@Rule public TestName name = new TestName()
def setupSpec() {}
def setup() {
driver.manage().window().maximize()
testloginname = name.methodName
println(testloginname) // this print the test case name ,works fine
}
def cleanup() {}
def cleanupSpec() {}
def "login tc4"(){
setup:
to HomePage
final String searchString = "String value";
searchInput = searchString;
when:
println("hellow world");
then:
If Anyone knows anyother solution then please suggest
英文:
I am using geb-spock
I have tried with Junit but found out that TestName does not @Before
code of GebConf.groovy file
caps.setCapability("name","");
I did not want to hard code the name
Note: If I leave name blank like about then session id shows as name
environments {
browserstackchrome {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "80.0");
caps.setCapability("resolution", "1366x768");
caps.setCapability("project", "KPNA_Geb");
caps.setCapability("build", "build 1");
caps.setCapability("name","");
println(login3.testloginname) // Print Null value
//getClass().getEnclosingMethod().getName()
driver = {
new RemoteWebDriver(new URL("http://" + username + ":" + accessKey + "@" + server + "/wd/hub"), caps)
};
}
Code of test case
class login3 extends GebReportingSpec{
public static String testloginname ;
@Rule public TestName name = new TestName()
def setupSpec() {}
def setup() {
driver.manage().window().maximize()
testloginname = name.methodName
println(testloginname) // this print the test case name ,works fine
}
def cleanup() {}
def cleanupSpec() {}
def "login tc4"(){
setup:
to HomePage
final String searchString = "String value"
searchInput = searchString
when:
println("hellow world")
then:
If Anyone knows anyother solution then please suggest
答案1
得分: 0
会话ID在BrowserStack
上显示为会话名称,实际上是默认值。如果您不希望这样,您可以将其设置为指定的值。
英文:
The session ID shown as the name of the session on BrowserStack
is actually the default value.<br>In case you don't want this, you can set this to a specified value.
答案2
得分: 0
protected void request(String sessionId, String nameOfTest) throws IOException {
URL url = new URL("https://api-cloud.browserstack.com/app-automate/sessions/" + sessionId + ".json");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("PUT");
httpConn.setRequestProperty("Content-Type", "application/json");
byte[] message = ("userName:accessKey").getBytes("UTF-8");
String basicAuth = DatatypeConverter.printBase64Binary(message);
httpConn.setRequestProperty("Authorization", "Basic " + basicAuth);
httpConn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
writer.write("{\"name\":\"" + nameOfTest + "\"}");
writer.flush();
writer.close();
httpConn.getOutputStream().close();
InputStream responseStream = httpConn.getResponseCode() / 100 == 2
? httpConn.getInputStream()
: httpConn.getErrorStream();
Scanner s = new Scanner(responseStream).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";
}
英文:
The only way to change name of test is sent request to browserstack, sessionId you can get with driver.getSessionId()
protected void request(String sessionId, String nameOfTest) throws IOException {
URL url = new URL("https://api-cloud.browserstack.com/app-automate/sessions/"+sessionId+".json");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("PUT");
httpConn.setRequestProperty("Content-Type", "application/json");
byte[] message = ("userName:accessKey").getBytes("UTF-8");
String basicAuth = DatatypeConverter.printBase64Binary(message);
httpConn.setRequestProperty("Authorization", "Basic " + basicAuth);
httpConn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
writer.write("{\"name\":\"" + nameOfTest + "\"}");
writer.flush();
writer.close();
httpConn.getOutputStream().close();
InputStream responseStream = httpConn.getResponseCode() / 100 == 2
? httpConn.getInputStream()
: httpConn.getErrorStream();
Scanner s = new Scanner(responseStream).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";
}
专注分享java语言的经验与见解,让所有开发者获益!
评论