LeanFT和Selenium兼容性?

时间:2015-10-26 18:48:41

标签: selenium architecture frameworks leanft

我想知道Selenium和LeanFT能否很好地融合在一起。我不知道是否还有人试图这样做,但我认为如果能够起作用,LeanFT可以为硒框架提供一些补充益处。

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

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

我已经尝试过针对HP建议的模型的对象流UML以及我自己对如何工作的想法。

HP Object Flow

Selenium and LeanFT Object Flow

控制流程将类似于:

  1. @Before - > globalSetup(LeanFT init)
  2. @BeforeClass - > testSetup(LeanFT init)
  3. @BeforeClass - > getSeleniumDriver(Selenium)
  4. @Test - > 一些硒程序 / ****防止硒死亡。 **** /
  5. @Test - >新主题 - >运行leanFTsnippet1()
  6. @Test - > 恢复selenium最后的步骤..
  7. @After - >报告,关闭Webdriver
  8. 以下是一些示例测试类的当前代码。

    @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());
    }
    

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

    谢谢!

3 个答案:

答案 0 :(得分: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
            });

....等等。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

我不完全确定为什么这个问题还没有得到公认的答案,但是我会用一个样本再一次强调 LeanFT和Selenium正在好好地回答这个问题。一起

它是用Java编写的。可能它可以进行一些优化,但它应该清楚地显示如何实现与同一浏览器的同步交互。

(Java项目是从LeanFT模板创建的.UnitTestClassBase类来自那里。它基本上初始化了LeanFT和幕后的记者。如果你不想使用它,你可以绕过它根据需要致电SDK.init()Reporter.init()Reporter.generateReport()SDK.cleanup() - check the docs for details

使用的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());
    }
}