当前位置: 首页 > 知识库问答 >
问题:

请建议这个selenium脚本与sauce实验室代码有什么问题

杨利
2023-03-14
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个答案

隗锐进
2023-03-14

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

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

https://wiki.saucelabs.com/display/docs/best+实践%3a+使用+环境+变量+用于+身份验证+凭据

 类似资料:
  • 请注意,在表单页和表单结果页上调用。 如果有人能在这个脚本中找出潜在的错误原因,我将不胜感激! 附注:我知道验证码脚本的缺点。我知道某些机器人仍然可以读出它们。我不想使用Recaptcha,因为它对我的用户来说太难了(不同的语言+很多次老用户)。我也知道md5很容易解密。 表单的结果 这工作得很好,所以非常感谢这个解决方案。 然而,我在这里看到了一些潜在的缺点。我注意到至少4个查询,感觉有点资源密

  • 我试图做一个代码,将打印所有元素的内容与从一些链接,但它不起作用,我不知道为什么,这是代码: 当我粘贴“someurl”而不是

  • 我在Excel中测试脚本实验室插件 我尝试对外部endpoint执行rest调用,如下所示 上面的工作正常。但在启用CORS的endpoint上,我得到了一个xhr.readyState 0。 如何避免CORS问题?我不是终端的主人

  • 问题内容: 我正在阅读DigitalOcean的golang客户程序。我注意到他们在变量中创建了* Op结构的实例。示例:https: //github.com/digitalocean/godo/blob/master/droplets.go#L32 为什么需要此行? 问题答案: 该行是*DropletsServiceOp满足DropletsService接口的编译时检查。 该行对程序的执行没有

  • 问题内容: 我已经为Employee类的父类是抽象的并且父类中的clone()方法是抽象的编写了此克隆方法。我想用此代码复制Employee对象的原始数据类型,而不是复制每个原始数据单独键入,但是此代码在我调用clone()方法的行中有问题。(此代码在Employee类中) 错误是:来自对象类型的方法clone()不可见。 但是我的Employee类在类层次结构中,可以访问Object类中受保护的

  • 错误: Test.ts(18,30):错误TS2345:类型为“{color1:String;}”的参数不能分配给类型为“squareConfig”的参数。对象文本只能指定已知属性,并且“SquareConfig”类型中不存在“Color1”。