2016-08-26 3 views
0

実際の結果が固定率であるテストシナリオがあります。例えば135.68%ですが、実際の結果と期待される結果を比較したい例えば130.00〜136.00の範囲にある。実際の結果が130.00から136.00の範囲にある場合、テストは合格です。それ以外の場合、テストは失敗します。Selenium Web Driver - Java - TestNG - Assert.assertEquals実際の結果を予想結果の範囲と比較する方法

import java.awt.Robot; 
    import java.awt.event.KeyEvent; 
    import java.io.File; 
    import java.io.FileInputStream; 
    import java.text.DateFormat; 
    import java.text.SimpleDateFormat; 
    import java.util.Date; 
    import jxl.Sheet; 
    import jxl.Workbook; 
    import org.apache.commons.io.FileUtils; 
    import org.openqa.selenium.By; 
    import org.openqa.selenium.OutputType; 
    import org.openqa.selenium.TakesScreenshot; 
    import org.openqa.selenium.WebElement; 
    import org.openqa.selenium.support.ui.Select; 
    import org.testng.Assert; 
    import org.testng.annotations.Test; 
    import org.testng.asserts.Assertion; 
    import org.testng.asserts.SoftAssert; 


    @Test 
    public class TC20042 extends BaseClass{ 

     private SoftAssert m_assert = new SoftAssert(); 


      public void registration() throws Exception, InterruptedException { 
        FileInputStream fi=new FileInputStream("C:\\File\\Book2.xls"); 
        Workbook w=Workbook.getWorkbook(fi); 
        Sheet s=w.getSheet(2); 


       try 
       { 
       for (int i = 0; i < s.getRows(); i++) 
       { 
       //Read data from excel sheet 
        String s1 = s.getCell(0,i).getContents(); 

    String xPath = "//*[@id='mainForm']/div/div[3]/div/div/table/tbody/tr[1]/td[6]"; 
        String text = driver.findElement(By.xpath(xPath)).getText(); 
        double percentage = Double.parseDouble(text); 

        assertThat(percentage).isBetween(130.0, 136.0); 

private Object assertThat(double percentage) { 
      // TODO Auto-generated method stub 
      return null; 
     } 



    } 

       } 
       catch(Exception e) 
       { 
       System.out.println(e); 

       } 
       } 

    } 
+0

はない 'driver.findElement(By.xpath(" // * [ID @ = 'アプリ-バナー']/div [1]/div/h2 "))。getText()'は135.68%を返しますか? – Shahid

+0

テキストをdoubleと 'assertTrue(130.0 <=実際の&&実際の<= 136.0)'として解析してみませんか? – beatngu13

+0

YesShahid - driver.findElement(By.xpath( "// * [@ * id = 'app-banner']&#47; div [1]/div 7; h2"))。getText()は、 135.68% – Curious

答えて

0

は私が定期的にTestNGのアサートを使用し、それは以下のように私の仕事:

String xPath = "//*[@id='mainForm']/div/div[3]/div/div/table/tbody/tr[1]/td[6]"; 
       String text = driver.findElement(By.xpath(xPath)).getText(); 
       text = text.replace("%", ""); 
       double percentage = Double.parseDouble(text); 

       Assert.assertTrue(percentage > 130.67 && percentage < 135.69); 
+0

これは既にあなたに提示されていました[ここ](http://stackoverflow.com/questions/39170029/selenium-web-driver-java-testng-assert-assertequals-how-to-compare-actual/39170860#comment65682759_39170029) [ここ](http://stackoverflow.com/a/39170336/3429133)。さらに、あなたが今使っている間隔はあなたの質問に使われた間隔ではありません。 – beatngu13

+0

私はAsserJインポートを使用せず、コード行はあなたが提案したものと少し異なります。 – Curious

+0

私のコメントとShahidsの答えはAssertJを使わなかったし、TestNGの 'assertTrue'だった。スニペットはわずかに異なるかもしれませんが、意味的には同じです。 – beatngu13

0

は単純に解析し、チェック:

String expectedPercentage = driver.findElement(By.xpath("//*[@id='app-banner']/div[1]/div/h2")).getText().replace("%", ""); 
    double percentage = Double.parseDouble(expectedPercentage); 

    // Do this parsing if range is in string format. Otherwise skip this block 
    String range = "130.00 to 136.00"; 
    String[] numbers = range.replaceAll(" ", "").split("to");   
    double min = Double.parseDouble(numbers[0]); 
    double max = Double.parseDouble(numbers[1]); 

    Assert.assertTrue(expectedPercentage + " is not between the range", min <= percentage && percentage <= max); 
+0

範囲を解析する理由は何ですか? – beatngu13

+0

の範囲は文字列 '' 130.00から136.00 "' – Shahid

+0

こんにちはSshahidで与えられているので、上のコードでどのようにWebからテキストを取得しますか?私はあなたがdriver.findElementを逃したことを意味する(By.xpath( "// * [@ id = 'app-banner']&#47; div [1]/div 7; h2"))。 () – Curious

0

テキストを解析した結果主張:

String xPath = "//*[@id='app-banner']/div[1]/div/h2"; 
String text = driver.findElement(By.xpath(xPath)).getText(); 
double percentage = Double.parseDouble(text.replace("%", "")); 

assertThat(percentage).isBetween(130.0, 136.0); 

このソリューションを、私は、次のassert文が、運を使用しています

AssertJからstatic importとして使用します。または、my commentで述べたTestNGアサーションを使用することもできます。

+0

こんにちはBeatngu13 - 上記のようにコードを更新しましたが、エラーが表示されます - 'メソッドのisBetween(double、double)はObject型に対して未定義です'オブジェクトを宣言する必要がありますか? – Curious

+0

@Curious次に、メソッドに間違った型を渡します。あなたのコードを更新してください。 – beatngu13

+0

こんにちはBeatngu13 - 私はコード上記更新が、エラーがまだこのライン \tで「複数のマーカーが表示されている - 方法isBetween(ダブルダブル) \t Object型 \tため未定義である - メソッドassertThat(ダブルする)のために定義されていませんタイプ \t TC20042 \t - トークンの構文エラー ")"このトークンを削除してください " – Curious

0
public static void assertEquals(double actual, 
           double expected, 
           double delta, 
           java.lang.String message) 
Asserts that two floats are equal concerning a delta. If they are not, an AssertionError, with the given message, is thrown. If the expected value is infinity then the delta value is ignored. 

assertEquals(133.00,expected,3.00,"ERROR") 
関連する問題