1

我想知道 Selenium 和 LeanFT 是否可以很好地配合使用。我不知道是否有人尝试过这样做,但我认为如果它可以工作,LeanFT 可以为 selenium 框架提供一些补充好处。

据我目前了解,Selenium 的局限性是:

  • Selenium 必须打开初始浏览器才能识别它
  • Selenium 必须打开所有弹出窗口以识别它们。
  • Selenium WebDriver 在等待非 Selenium 过程时可能会变得陈旧。

我已经为 HP 建议的模型和我自己对它如何工作的想法尝试了对象流 UML。

HP 对象流

Selenium 和 LeanFT 对象流

控制流将类似于:

  1. @Before -> globalSetup (LeanFT init)
  2. @BeforeClass -> testSetup (LeanFT init)
  3. @BeforeClass -> getSeleniumDriver (Selenium)
  4. @Test ->一些 selenium 程序 /**** 防止 Selenium 死亡。****/
  5. @Test -> 新线程 -> 运行leaFTsnippet1()
  6. @Test ->恢复硒的最后一步..
  7. @After -> 报告,关闭 Webdriver

这是来自示例测试类的一些当前代码。

@BeforeClass
public static void beforeLFTClass() throws Exception {
    globalSetup(CoreFunctionality.class);
}

@AfterClass
public static void afterLFTClass() throws Exception {
    globalTearDown();
}

@Test
public void runLeanFtThread() {
    // put selenium code here
             // ...
             // begin leanft part of test
    Thread leanftThread = new Thread( new Runnable() {

        @Override
        public void run() {
            try {
                test();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    leanftThread.start();
    try {
        leanftThread.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


public void test() throws Exception {

    //Starting Browser
    Browser browser = BrowserFactory.attach(new BrowserDescription.Builder().title(driver.getTitle()).build());
    Assert.assertEquals(browser.getTitle(), driver.getTitle());
}

无论如何,这是一个非常有趣的问题。真的很想看看你们的想法。

谢谢!

4

3 回答 3

1

他们确实一起玩得很好。我一直在我的脚本中使用它们,我喜欢利用每个工具的功能。我所做的是创建一个 LeanFT 测试模板并将 Selenium 库添加到其中。

这是一个示例代码:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HP.LFT.SDK;
using HP.LFT.SDK.Web;
using Search_Regression_Test;
using TestAutomationReporting;
using UnifiedFramework;
using System.Configuration;
using System.Diagnostics;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using Selenium = OpenQA.Selenium;
namespace Search_Regression_Test
{
    [TestClass]
    public class LeanFtTest : UnitTestClassBase<LeanFtTest>
    {        
        static IBrowser browser;        
        static IWebDriver chromeDriver;

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {

            GlobalSetup(context);                        
            ChromeOptions CO = new ChromeOptions();
            CO.AddExtension(@"C:\Program Files (x86)\HP\LeanFT\Installations\Chrome\Agent.crx");
            chromeDriver = new ChromeDriver(CO);
            chromeDriver.Manage().Window.Maximize();

            browser = BrowserFactory.Attach(new BrowserDescription
            {
                Type = BrowserType.Chrome
            });

.... 等等。

于 2015-12-09T16:10:09.883 回答
1

新版本的 LeanFT (14) 甚至带来了一些明确的 Selenium 集成:您可以在项目创建向导中选择 Selenium 作为您的自动化 SDK,有一个特定于 Selenium 的对象识别中心,以及一些额外的定位器和实用程序。全文在这里:Selenium 的 LeanFT

于 2017-02-17T07:51:34.263 回答
0

我不完全确定为什么这个问题还没有一个公认的答案,但我将尝试用一个样本来回答这个问题,该样本再次强调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());
    }
}
于 2017-08-25T06:41:44.320 回答