请使用酱汁实验室代码建议此硒脚本出了什么问题

时间:2018-10-10 05:01:49

标签: javascript selenium-webdriver jenkins-plugins saucelabs

  

失败的配置:@BeforeClass createDriver   org.openqa.selenium.WebDriverException:无法解析远程响应:配置错误– Sauce Labs身份验证错误。   您使用用户名'null'和访问密钥'null'进行身份验证,这不是有效的Sauce Labs凭据。   收到了以下所需功能:   {'browserName':'Chrome',    'build':'Selenium_Soucelabs_2',    '名称':'在Windows 10上测试',    'passed':'true',    'platform':'WIN10',    'version':'66 .0'}

这里我无法使用此配置运行脚本,请帮助我进行测试的地方出了问题。我使用的是调味汁实验室,该代码中未提及的脚本中的testng,我从此处开始跟踪脚本:” https://github.com/saucelabs-sample-test-frameworks/Java-TestNG-Selenium/blob/master/src/test/java/com/yourcompany/Tests/TestBase.java

package Checkout;

import org.testng.asserts.SoftAssert;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.UnexpectedException;
import java.util.concurrent.TimeUnit;
import org.testng.ITestResult;
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;


public class TestBaseFR {
	Object verificationErrors;
	private ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();
	private ThreadLocal<String> sessionId = new ThreadLocal<String>();
	SoftAssert softAssert=new  SoftAssert();  
	
	
	public static final String BUILDTAG = System.getenv("BUILD_TAG");

		// enter your saucelabs user name here
public static final String USERNAME = System.getenv("SAUCE_USERNAME");
// enter your access key here
public static final String ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
	public static final String SauceLabURL = "http://" + USERNAME + ":" + ACCESS_KEY
			+ "@ondemand.saucelabs.com:80/wd/hub";
	
	/**
     * DataProvider that explicitly sets the browser combinations to be used.
     *
     * @param testMethod
     * @return Two dimensional array of objects with browser, version, and platform information
     */
    @DataProvider(name = "hardCodedBrowsers", parallel = true)
    public static Object[][] sauceBrowserDataProvider(Method testMethod) {
        return new Object[][]{
                new Object[]{"MicrosoftEdge", "14.14393", "Windows 10"},
                new Object[]{"firefox", "49.0", "Windows 10"},
                new Object[]{"internet explorer", "11.0", "Windows 7"},
                new Object[]{"safari", "10.0", "OS X 10.11"},
                new Object[]{"chrome", "54.0", "OS X 10.10"},
                new Object[]{"firefox", "latest-1", "Windows 7"},
        };
    }

	public WebDriver driver;
	/**
     * @return the {@link WebDriver} for the current thread
     */
    public WebDriver getWebDriver() {
        return webDriver.get();
    }
	 /**
    *
    * @return the Sauce Job id for the current thread
    */
   public String getSessionId() {
       return sessionId.get();
   }

	/**
	 * @throws java.lang.Exception
	 */

	protected void CreateDriver(String browser, String version, String os, String methodName)
            throws MalformedURLException, UnexpectedException {
		DesiredCapabilities caps = new DesiredCapabilities();
		caps.setCapability(CapabilityType.BROWSER_NAME, browser);
		caps.setCapability(CapabilityType.VERSION,version);
		caps.setCapability(CapabilityType.PLATFORM_NAME,os);
		caps.setCapability("name",methodName);
		 driver = new RemoteWebDriver(new URL(SauceLabURL), caps);
        if (BUILDTAG != null) {
        	caps.setCapability("build", BUILDTAG);
        }

        // Launch remote browser and set it as the current thread
        webDriver.set(new RemoteWebDriver(
                new URL("https://" + USERNAME + ":" + ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub"),
                caps));

        // set current sessionId
        String id = ((RemoteWebDriver) getWebDriver()).getSessionId().toString();
        sessionId.set(id);
    }

		
  

	// Test Results
	@AfterMethod(alwaysRun = true)
	public void tearDown(ITestResult result) throws Exception {
		
		 
		        ((JavascriptExecutor) webDriver.get()).executeScript("sauce:job-result=" + (result.isSuccess() ? "passed" : "failed"));
		        webDriver.get().quit();
		    }

		    protected void annotate(String text) {
		        ((JavascriptExecutor) webDriver.get()).executeScript("sauce:context=" + text);
		    
	}


	

}

1 个答案:

答案 0 :(得分:0)

似乎您需要设置saucelab环境变量。

  1. 单击任务栏上的开始。
  2. 对于搜索程序和字段,输入环境变量。
  3. 单击“编辑环境变量”。 这将打开“系统属性”对话框。
  4. 单击环境变量。 这将打开“环境变量”对话框。
  5. 在“系统变量”部分中,单击“新建”。 这将打开“新建系统变量”对话框。
  6. 对于变量名,输入SAUCE_USERNAME。
  7. 对于“变量值”,输入您的Sauce用户名。
  8. 单击“确定”。
  9. 重复4-8以设置SAUCE_ACCESS_KEY。

https://wiki.saucelabs.com/display/DOCS/Best+Practice%3A+Use+Environment+Variables+for+Authentication+Credentials

相关问题