我不完全确定为什么这个问题还没有一个公认的答案,但我将尝试用一个样本来回答这个问题,该样本再次强调LeanFT 和 Selenium 一起玩得很好
它是用 Java 编写的。也许它可以优化一点,但它应该清楚地显示如何实现与同一浏览器的同时交互。
(Java 项目是从 LeanFT 模板创建的。UnitTestClassBase 类来自那里。它基本上在幕后初始化 LeanFT 和报告器。如果你不想使用它,要绕过它,你必须调用SDK.init()
,和as需要 -检查文档以获取详细信息)Reporter.init()
Reporter.generateReport()
SDK.cleanup()
使用的 AUT 是优势购物:http ://advantageonlineshopping.com/
package com.demo;
import static org.junit.Assert.*;
import java.io.File;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.Keys;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.hpe.leanft.selenium.By;
import com.hp.lft.report.Reporter;
import com.hp.lft.report.Status;
import com.hp.lft.sdk.web.*;
import com.hp.lft.verifications.Verify;
public class SeleniumTest extends UnitTestClassBase {
private ChromeDriver chromeDriver;
private Browser browser;
public SeleniumTest() {
System.setProperty("webdriver.chrome.driver",this.getClass().getResource("/chromedriver.exe").getPath());
}
@BeforeClass
public static void setUpBeforeClass() throws Exception {
instance = new SeleniumTest();
globalSetup(SeleniumTest.class);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
globalTearDown();
}
@Before
public void setUp() throws Exception {
// SELENIUM: Construct and launch the browser with LeanFT agent
ChromeOptions options = new ChromeOptions();
File paths = new File("C:\\Program Files (x86)\\HP\\LeanFT\\Installations\\Chrome\\Agent.crx");
options.addExtensions(paths);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
chromeDriver = new ChromeDriver(options);
}
@After
public void tearDown() throws Exception {
// LEANFT: close the browser opened by selenium
browser.close();
}
@Test
public void test() throws Exception {
// SELENIUM: Go to the Advantage shopping website and maximize it
chromeDriver.get("http://156.152.164.67:8080/#/");
chromeDriver.manage().window().maximize();
// LEANFT: Attach to the browser
browser = BrowserFactory.attach(new BrowserDescription.Builder()
.type(BrowserType.CHROME).openTitle(" Advantage Shopping")
.build());
// LEANFT: Click on tablets button
browser.describe(WebElement.class, new WebElementDescription.Builder()
.className("categoryCell").tagName("DIV").innerText("TABLETS Shop Now ").build()).click();
// SELENIUM: Expand the display section after it was seen
(new WebDriverWait(chromeDriver, 10))
.until(new ExpectedCondition<org.openqa.selenium.WebElement>(){
@Override
public org.openqa.selenium.WebElement apply(org.openqa.selenium.WebDriver d) {
return d.findElement(By.cssSelector("h4#accordionAttrib0"));
}}).click();
// LEANFT: select the preferred display size, click the preferred tablet and add the tablet to the cart
browser.describe(CheckBox.class, new CheckBoxDescription.Builder()
.type("checkbox").role("").accessibilityName("").tagName("INPUT").name("").index(1).build()).set(true);
browser.describe(Image.class, new ImageDescription.Builder()
.alt("").type(com.hp.lft.sdk.web.ImageType.NORMAL).tagName("IMG").index(1).build()).click();
browser.describe(Button.class, new ButtonDescription.Builder()
.buttonType("submit").tagName("BUTTON").name("ADD TO CART").build()).click();
// SELENIUM: go to cart
chromeDriver.get("http://156.152.164.67:8080/#/shoppingCart");
// LEANFT: checkout
browser.describe(Button.class, new ButtonDescription.Builder()
.buttonType("submit").tagName("BUTTON").name("CHECKOUT ($1,009.00)").build()).click();
// SELENIUM: Register as a new user after the button was seen
(new WebDriverWait(chromeDriver, 10))
.until(new ExpectedCondition<org.openqa.selenium.WebElement>(){
@Override
public org.openqa.selenium.WebElement apply(org.openqa.selenium.WebDriver d) {
return d.findElement(By.xpath("//DIV[@id=\"newClient\"]/DIV[1]/SEC-FORM[1]/SEC-SENDER[1]/BUTTON[@role=\"button\"][1]"));
}}).click();
// LEANFT: fill in the user name and email
String username = "U" + Calendar.getInstance().getTimeInMillis(); // unique name each time
browser.describe(EditField.class, new EditFieldDescription.Builder()
.type("text").tagName("INPUT").name("userName").build()).setValue(username);
browser.describe(EditField.class, new EditFieldDescription.Builder()
.type("text").tagName("INPUT").name("userEmail").build()).setValue("myuser_email@emailsite.org");
// SELENIUM: Set password and confirm password
chromeDriver.findElementByXPath("//SEC-VIEW/DIV[normalize-space()=\"*Password\"]/INPUT[1]").sendKeys("Password1");
chromeDriver.findElementByXPath("//SEC-VIEW/DIV[normalize-space()=\"*Confirm password\"]/INPUT[1]").sendKeys("Password1");
// LEANFT: check the 'I agree' checkbox and register, then click on next shipping details.
browser.describe(CheckBox.class, new CheckBoxDescription.Builder()
.type("checkbox").tagName("INPUT").name("registrationAgreement").build()).set(true);
browser.describe(Button.class, new ButtonDescription.Builder()
.buttonType("button").tagName("BUTTON").name("REGISTER").build()).click();
browser.describe(Button.class, new ButtonDescription.Builder()
.buttonType("submit").tagName("BUTTON").name("NEXT").build()).click();
// SELENIUM: confirm the user name and pass
chromeDriver.findElementByXPath("//DIV[@id=\"paymentMethod\"]/DIV/DIV/SEC-FORM/SEC-VIEW/DIV[normalize-space()=\"*SafePay username\"]/INPUT[1]").sendKeys(username);
chromeDriver.findElementByXPath("//DIV[@id=\"paymentMethod\"]/DIV/DIV/SEC-FORM/SEC-VIEW/DIV[normalize-space()=\"*SafePay password\"]/INPUT[1]").sendKeys("Password1");
// LEANFT: click "Pay now" and confirm payment was done
browser.describe(Button.class, new ButtonDescription.Builder()
.buttonType("button").role("button").accessibilityName("").tagName("BUTTON").name("PAY NOW").index(0).build()).click();
Verify.isTrue(
browser.describe(WebElement.class, new WebElementDescription.Builder()
.tagName("SPAN").innerText("Thank you for buying with Advantage").build())
.exists());
}
}