在 browserstack 的 name capability(会话)上传递测试案例名称,使用 geb-spock。

huangapple 未分类评论43阅读模式
英文:

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(&quot;https://api-cloud.browserstack.com/app-automate/sessions/&quot;+sessionId+&quot;.json&quot;);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestMethod(&quot;PUT&quot;);
        httpConn.setRequestProperty(&quot;Content-Type&quot;, &quot;application/json&quot;);

        byte[] message = (&quot;userName:accessKey&quot;).getBytes(&quot;UTF-8&quot;);
        String basicAuth = DatatypeConverter.printBase64Binary(message);
        httpConn.setRequestProperty(&quot;Authorization&quot;, &quot;Basic &quot; + basicAuth);

        httpConn.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
        writer.write(&quot;{\&quot;name\&quot;:\&quot;&quot; + nameOfTest + &quot;\&quot;}&quot;);
        writer.flush();
        writer.close();
        httpConn.getOutputStream().close();
              InputStream responseStream = httpConn.getResponseCode() / 100 == 2
                ? httpConn.getInputStream()
                : httpConn.getErrorStream();
        Scanner s = new Scanner(responseStream).useDelimiter(&quot;\\A&quot;);
        String response = s.hasNext() ? s.next() : &quot;&quot;;
    }

huangapple
  • 本文由 发表于 2020年7月27日 00:55:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/63103108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定