首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于Java的Web应用测试自动化框架

基于Java的Web应用测试自动化框架
EN

Stack Overflow用户
提问于 2013-11-17 06:37:23
回答 8查看 22.9K关注 0票数 2

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

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

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

我搜索了这个特定的场景,但没有找到。我知道将会有大量的集成,插件,等等。这是需要建立的。我的问题是,你能提供一些建议(即使是好的阅读也可以),开始使用开源解决方案构建这个框架吗?

EN

回答 8

Stack Overflow用户

发布于 2013-11-18 01:32:07

  • Selenium将允许您自动执行所有web (浏览器)操作。
  • Junit/TestNG作为测试框架,包括用于项目管理和生命周期(包括使用surefire plugin的测试阶段)的默认报告系统
  • Maven
  • Jenkins是一个很好的集成工具,可以轻松运行

上的设置

祝好运!

票数 4
EN

Stack Overflow用户

发布于 2014-05-14 13:06:10

我在这里给出了框架函数,它大大减少了代码

代码语言:javascript
复制
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();
     }
票数 1
EN

Stack Overflow用户

发布于 2013-11-18 07:05:20

只要您有启动框架的命令行,并且使用xunit日志格式进行报告,那么您就可以很好地与任意数量的持续集成框架集成。

在负载下运行浏览器实例的权衡将是每台主机上的虚拟用户更少,以及在负载下非常仔细地检查负载生成器资源。不要忘记在您的框架中包含监控API,用于负载下的系统指标,以及与SLA指标验收相关的自动评估引擎,以确定在给定负载点的负载下失败标准的通过情况。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20024811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档