使用Java测试Web应用程序的自动化框架

时间:2013-11-16 22:37:23

标签: selenium-webdriver jenkins-plugins hudson-plugins gui-test-framework

我开始用我的Web应用程序编写Java测试自动化框架(我很熟悉的语言)。目前,它在UI上完全测试。近期没有后端/ API测试。

我计划使用Selenium Web Driver。该框架将支持功能/集成和性能测试。

我第一次使用开源解决方案构建(使用像LoadRunner这样的工具),我的需求是这个框架将与Jenkins / Hudson等持续集成工具和用于报告结果的内部测试管理工具一起使用。 / p>

我搜索了这个特定的场景但找不到。我知道会有很多集成,插件等......需要构建。我的问题是你能否开始用开源解决方案构建这个框架,提供一些指导(即使是好读也行)?

8 个答案:

答案 0 :(得分:4)

  • Selenium将允许您自动执行所有Web(浏览器)操作 自动化。
  • Junit / TestNG作为测试框架, 包括他们的默认报告系统
  • Maven用于该项目 管理和生命周期(包括surefire plugin的测试阶段)
  • Jenkins是一款易于使用的优秀集成工具 运行上面的设置
祝你好运!

答案 1 :(得分:1)

我在这里给出的框架函数非常能减少代码

public TestBase() throws Exception{
    baseProp = new Properties();
    baseProp.load(EDCPreRegistration.class.getResourceAsStream("baseproperties.properties"));

    // Firefox profile creation
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("network.proxy.type", ProxyType.AUTODETECT.ordinal());
    profile.setPreference("browser.cache.disk.enable", false); 

    profile.setPreference("network.proxy.http", "localhost");
    profile.setPreference("network.proxy.http_port",8080);
    driver = new FirefoxDriver(profile);
    //System.setProperty("webdriver.ie.driver","E:\\Phyweb Webdriver\\IEDriverServer.exe");
    //driver = new InternetExplorerDriver();
    driver.manage().window().maximize();
}

 //To find WebElement by id
  public static WebElement FindElement(String id)
  {
      try
      {
            webElement= driver.findElement(By.id(id));
      }
      catch(Exception e)
      {
          Print(e);
      }
      return webElement;
  }

  //To find WebElement by name
  public static WebElement FindElementByName(String name)
  {
      try
      {
            webElement= driver.findElement(By.name(name));
      }
      catch(Exception e)
      {
          Print(e);
      }
       return webElement;
  }

  //To find WebElement by Class
  public static WebElement FindElementByClass(String classname)
  {
      try
      {
            webElement= driver.findElement(By.className(classname));
      }
      catch(Exception e)
      {
          Print(e);
      }
       return webElement;
  }

//To get data of a cell
public static String GetCellData(XSSFSheet sheet,int row,int col)
{
    String cellData = null;
    try 
    {
        cellData=PhyWebUtil.getValueFromExcel(row, col, sheet);
    }
    catch (Exception e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return cellData;
}

    //To click a button using id
    public static void ClickButton(String id,String label)
    {
        try
        {
                WebElement webElement= FindElement(id);
                Snooze();
                webElement.click();
                PrintMessage(label+" is selected");
        }
        catch(Exception e)
        {
            Print(e);
        }
    }   

            //To click a button using class
            public void ClickButtonByClass(String classname,String label)
            {
                try
                {
                        WebElement webElement= FindElementByClass(classname);
                        Snooze();
                        webElement.click();
                        PrintMessage(label+" is selected");
                }
                catch(Exception e)
                {
                    Print(e);
                }
            }   
     //To enter data into Textbox
     public String editTextField(int rownum, int celnum,WebElement element ,XSSFSheet sheet,String Label)
      {
            XSSFRow row = sheet.getRow(rownum);
            XSSFCell Cell = row.getCell(celnum);
            String inputValue = Cell.getStringCellValue().trim();
            element.clear();//To clear contents if present
            try
            {
                      element.sendKeys(inputValue);
                      String  elementVal=element.toString();
                      if(elementVal.contains("password"))
                      {   
                          PrintMessage("Password is entered");
                      }
                      else
                      {   
                          PrintMessage("Value entered for "+Label+" is "+inputValue);
                      }  
            }
            catch(Exception e){
                Print(e);
                //cv.verifyTrue(false, "<font color= 'red'> Failed due to : </font> "+e.getMessage());
            }
            return inputValue;
      }

    //To enter data into Textbox
     public String editTextFieldDirect(WebElement element ,String text,String label)
      {
            element.clear();//To clear contents if present
            try
            {
                      element.sendKeys(text);
                      String  elementVal=element.toString();
                      if(elementVal.contains("password"))
                      {   
                          PrintMessage("Password is entered");
                      }
                      else
                      {   
                          PrintMessage("Value entered for "+label+" is "+text);
                      }  
            }
            catch(Exception e){
                Print(e);
                //cv.verifyTrue(false, "<font color= 'red'> Failed due to : </font> "+e.getMessage());
            }
            return text;
      }
        //To select Radio button
        public void ClickRadioButton(String id)
        {
            try
            {
                    WebElement webElement= FindElement(id);
                    Snooze();
                    webElement.click();                 
                    text=webElement.getText();          
                    PrintMessage(text+" is selected");
            }
            catch(Exception e)
            {
                Print(e);
            }
        } 

    //To select Link
    public void ClickLink(String id,String label)
    {
        try
        {
                ClickButton(id,label);
        }
        catch(Exception e)
        {
            Print(e);
        }
    } 

  //To Click an Image button
    public void ClickImage(String xpath)
    {
        try
        {
                WebElement webElement= FindElement(id);
                Snooze();
                webElement.click();
                text=GetText(webElement);
                PrintMessage(text+" is selected");
        }
        catch(Exception e)
        {
            Print(e);
        }
    } 

    //Select a checkbox
    public void CheckboxSelect(String id,String label)
    {
        try
        {
                WebElement webElement= FindElement(id);
                Snooze();
                webElement.click();
                PrintMessage("Checkbox "+label+" is selected");
        }
        catch(Exception e)
        {
            Print(e);
        }
    } 

    //To select value in Combobox
    public void SelectData(String id,String label,String cellval)
    {
        try
        {
                WebElement webElement= FindElement(id);
                Snooze();
                webElement.click();
                String elementStr=webElement.toString();
                int itemIndex=elementStr.indexOf("value");
                if(itemIndex>-1)
                {   
                    int endIndex=elementStr.length()-3;
                    String item=elementStr.substring(itemIndex+7, endIndex);
                    if(cellval=="0")
                    {   
                         PrintMessage(item+" is selected for "+label);
                    }
                    else
                    {
                        PrintMessage(cellval+"  "+label+" is selected");
                    }
                }   
                else
                {
                    PrintMessage(cellval+" is selected for "+label);
                }
        }
        catch(Exception e)
        {
            Print(e);
        }
    } 

      //To check if WebElement with id exists
      public static boolean isExists(String id) 
      {
          boolean exists = false;
          driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
              try 
              {
                       exists=driver.findElements( By.id(id) ).size() != 0;    
              } 
              catch (Exception e) 
              {
                  Print(e);
             }
              if(exists==true)


               return true;


              else


               return false;
      }

      //To check if WebElement with name exists
      public static boolean isExistsName(String name) 
      {
          boolean exists = false;
          driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
              try 
              {
                       exists=driver.findElements( By.name(name) ).size() != 0;    
              } 
              catch (Exception e) 
              {
                  if(e.getMessage().contains("InvalidSelectorError"))
                  {   
                      System.out.println("");
                  }
                  else
                      Print(e);
             }
              if(exists==true)


               return true;


              else


               return false;
      }

        //Explicit wait until a element is visible and enabled using id
        public void ExplicitlyWait(String id)
          {
              try
              {
                  WebElement myDynamicElement = (new WebDriverWait(driver, 10))
                            .until(ExpectedConditions.presenceOfElementLocated(By.id(id)));
              }
              catch(Exception e)
              {
                  Print(e);
              }
          }

        //Explicit wait until a element is visible and enabled using classname
        public void ExplicitlyWaitByClass(String classname)
          {
              try
              {
                  WebElement myDynamicElement = (new WebDriverWait(driver, 10))
                            .until(ExpectedConditions.presenceOfElementLocated(By.className(classname)));
              }
              catch(Exception e)
              {
                  Print(e);
              }
          }

        //Explicit wait until a element is visible and enabled using id
        public void ExplicitlyWaitSpecific(int sec,String id)
          {
              try
              {
                  WebElement myDynamicElement = (new WebDriverWait(driver, sec))
                            .until(ExpectedConditions.presenceOfElementLocated(By.id(id)));
              }
              catch(Exception e)
              {
                  Print(e);
              }
          }

    //Snooze for 10 seconds
    public static void Snooze()
      {
          try
          {
              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          }
          catch(Exception e)
          {
              Print(e);
          }
      }

    //Snooze for Secs
    public static void SnoozeSpecific(int seconds)
      {
          try
          {
              driver.manage().timeouts().implicitlyWait(seconds, TimeUnit.SECONDS);
          }
          catch(Exception e)
          {
              Print(e);
          }
      }

    //Sleep for milliSeconds
    public static void Sleep(int milisec) throws InterruptedException
    {
        Thread.sleep(milisec);
    }

    //To get text using text()
     public static String GetText(WebElement element)
     {
         try
         {  
             text=element.getText();
         }   
         catch(Exception e){


             Print(e);
         }
        return text;
     }

     //To get text using getAttribute("value")
     public static String GetTextAttribute(WebElement element)
     {
         try
         {  
             text=element.getAttribute("value");
         }   
         catch(Exception e){


             Print(e);
         }
        return text;
  }
     //To Print error messages to both Console and Results file
     public static void Print(Exception e)
     {
         Reporter.log("Exception is :"+e.getMessage());
         System.out.println(e);
     }

     //To Print messages to both Console and Results file
     public static void PrintMessage(String str)
     {
         Reporter.log(str);
         System.out.println(str);
     }

    //To Print Blank row
     public static void BlankRow()
     {
         Reporter.log("                                              ");
         System.out.println("                                              ");
     }

    //To Print Sub header
     public static void Header(String str)
     {
         BlankRow();
         Reporter.log("***********************"+str+" Verifications***********************");
         System.out.println("***********************"+str+" Verifications***********************");
         BlankRow();
     }

    //To Print Sub header
     public static void SubHeader(String str)
     {
         BlankRow();
         Reporter.log("-----------------------"+str+" Verifications-----------------------");
         System.out.println("-----------------------"+str+" Verifications-----------------------");
         BlankRow();
     }

答案 2 :(得分:0)

只要您有一个命令行来启动框架并使用xunit日志格式进行报告,那么您应该适合与任意数量的持续集成框架集成。

在负载下运行浏览器实例时,您需要权衡的是每个主机的虚拟用户数量减少,以及负载下负载生成器资源的仔细检查。不要忘记在框架中包含监控API以获取负载下的系统指标和与SLA指标接受相关的自动评估引擎,以确定在给定负载点下负载下的失败标准的传递。

答案 3 :(得分:0)

我们正在开始开发与您的需求非常相关的东西; Java,Webdriver,Jenkins,Maven等。我们在这里对自动化很新,但仍然有很好的Java资源。 我们正在从www.seleniumtests.com建立基于Tarun Kumar的框架。 他有很多来自Youtube的好视频(听起来质量不太好),并且他设法使用PageObjects Pattern创建非常用户友好的东西。 如果你没有任何线索从哪里开始,我会从那里开始。 祝你好运!

答案 4 :(得分:0)

Selenium Webdriver - Selenium是一个基于Web的自动化工具,可以自动化网页上的所有内容和所有内容。你使用Selenium Webdriver和JAVA。

Watij - 使用Java进行Web应用程序测试 通过真实的Web浏览器自动化Web应用程序的功能测试。

Gatling - 用于性能测试和压力测试

Jenkins - Jenkins为软件开发提供持续集成服务。

答案 5 :(得分:0)

Selenium-Webdriver肯定是UI自动化的工具,我们广泛使用它来对浏览器堆栈等云解决方案进行跨浏览器测试。

我们的用例让我们使用TestNG作为测试运行器构建了一个用Java构建的开源框架“ omelet ”,它负责处理与Web测试相关的所有内容并让我们实际上自动化应用程序,而不是考虑报告,并行运行和CI集成等

建议,总是欢迎贡献:)

here和{。}上的文档 Github链接here

请记得在网站上查看5分钟的教程

答案 6 :(得分:0)

我在selenium的顶部创建了一个java库,简化了网站的测试自动化。它有一个隐含的等待机制,易于使用:

https://github.com/gartenkralle/web-ui-automation

示例:

import org.junit.Test;
import org.openqa.selenium.By;

import common.UserInterface;
import common.TestBase;

public class Google extends TestBase
{
    private final static String GOOGLE_URL = "https://www.google.com/";

    private final static By SEARCH_FIELD = By.xpath("//input[@id='lst-ib']");
    private final static By AUTO_COMPLETION_LIST_BOX = By.xpath("//*[@id='sbtc']/div[2][not(contains(@style,'none'))]");
    private final static By SEARCH_BUTTON = By.xpath("//input[@name='btnK']");

    @Test
    public void weatherSearch()
    {
        UserInterface.Action.visitUrl(GOOGLE_URL);
        UserInterface.Action.fillField(SEARCH_FIELD, "weather");
        UserInterface.Verify.appeared(AUTO_COMPLETION_LIST_BOX);
        UserInterface.Action.pressEscape();
        UserInterface.Action.clickElement(SEARCH_BUTTON);
    }
}

答案 7 :(得分:0)

用于功能回归测试:

TestProject
Selenium
Cucumber:这是BDD工具

对于非功能性:性能和负载测试:

JMeter

注意: TestComplete是非常好的商业工具。