Soft Assertion for Selenium WebDriver With TestNG

Embed Size (px)

Citation preview

  • 8/18/2019 Soft Assertion for Selenium WebDriver With TestNG

    1/4

    Soft Assertion For Selenium WebDriver With TestNG

    If you know, we have already learn about testng hard assertions !"am#leassert!$uals, assertNot!$uals, et%&&' whi%h we %an use In our webdriver test&

    (ard assertion e"am#les links are given on T(IS )AG!& *iew all thosee"am#les one by one to learn hard assertions& +et me tell you one thingabout hard assertions, When hard assertion will fail Inside any test method,remaining e"e%ution of that s#e%i% test method will be aborted& Now If youwants to %ontinue remaining test #art e"e%ution even If assertion fails andalso you wants to re#ort assertion failure In testng result re#ort then you %anuse testng soft assertion method&

    Soft Assertions In Selenium WebDriver To use testng soft assertion, you have to use testng SoftAssert %lass& This%lass will hel#s to not throw an e"%e#tion on assertion failure and re%ording

    failure& If you will use soft assertion then your test e"e%ution will remain%ontinue even If any assertion fails& Another most Im#ortant thing Is yourassertion failure will be re#orted In re#ort so that you %an view It at end of test& -ou %an use soft assertion when you are using multi#le assertions Insame test method and you wants to e"e%ute all of them even If any one Inbetween fails&

    +et us look at very sim#le e"am#le of testng hard assertion and softassertion to see the di.eren%e between both of them& In bellow givee"am#le, hard/assert/te"t' and soft/assert/te"t' ea%h have 0 assertions&1sed hard assertions In hard/assert/te"t' method and soft assertions In

    soft/assert/te"t' method to des%ribe di.eren%e between both of them&

    2un bellow given e"am#le In your e%li#se with testng framework&

    #a%kage Testng/)a%k3

    im#ort org&o#en$a&selenium&4y3im#ort org&o#en$a&selenium&WebDriver3im#ort org&o#en$a&selenium&refo"&Firefo"Driver3im#ort org&o#en$a&selenium&su##ort&ui&!"#e%ted5onditions3

    im#ort org&o#en$a&selenium&su##ort&ui&WebDriverWait3im#ort org&testng&Assert3im#ort org&testng&annotations&After5lass3im#ort org&testng&annotations&4efore5lass3im#ort org&testng&annotations&Test3im#ort org&testng&asserts&SoftAssert3

    #ubli% %lass Soft/Assert 6

    http://software-testing-tutorials-automation.blogspot.in/2014/03/assertassertequals-testng-with-selenium.htmlhttp://software-testing-tutorials-automation.blogspot.in/2014/03/example-of-assertnotequals-in-selenium.htmlhttp://software-testing-tutorials-automation.blogspot.in/2014/05/selenium-webdriver-tutorials-part-two.htmlhttp://software-testing-tutorials-automation.blogspot.in/2014/03/example-of-assertnotequals-in-selenium.htmlhttp://software-testing-tutorials-automation.blogspot.in/2014/05/selenium-webdriver-tutorials-part-two.htmlhttp://software-testing-tutorials-automation.blogspot.in/2014/03/assertassertequals-testng-with-selenium.html

  • 8/18/2019 Soft Assertion for Selenium WebDriver With TestNG

    2/4

      775reated ob8e%t of testng SoftAssert %lass to use It9s )ro#erties&  SoftAssert s/assert : new SoftAssert'3  String A%tualte"t3  WebDriver driver : new Firefo"Driver'3 

    ;4efore5lass  #ubli% void load/url'6  driver&manage'&window'&ma"imi

  • 8/18/2019 Soft Assertion for Selenium WebDriver With TestNG

    3/4

      System&outrintln=Soft Assertion ?E Bst #agete"t assertion e"e%uted&='3 

    s/assert&assert!$ualsA%tualte"t, =Tuesday, @ anuary @B0=, =@nd assertfailed&='3  System&outrintln=Soft Assertion ?E @nd #agete"t assertion e"e%uted&='3

    driver&nd!lement4y&"#ath=77in#ut;value:9Show He Alert9=''&%li%k'3  String Alert/te"t : driver&swit%hTo'&alert'&getTe"t'3  driver&swit%hTo'&alert'&a%%e#t'3 

    77Alert e"#e%ted te"t Is written In%orre%t intentionally to get fail thisassertion&  s/assert&assert!$ualsAlert/te"t, =(i&& is alert messageJ=, =Alert IsIn5orre%t='3  System&outrintln=Soft Assertion ?E Bst alert assertion e"e%uted&='3 

    s/assert&assert!$ualsAlert/te"t, =(i&& This is alert messageJ=, =Alert Is5orre%t='3  System&outrintln=Soft Assertion ?E @nd alert assertion e"e%uted&='3  s/assert&assertAll'3  C

    ;Test  #ubli% void wait/and/%li%k'6  WebDriverWait wait : new WebDriverWaitdriver, BK'3 wait&until!"#e%ted5onditions&elementTo4e5li%kable4y&"#ath=77in#ut;id:9submit4utton9='''3  driver&nd!lement4y&"#ath=77in#ut;id:9submit4utton9=''&%li%k'3  C

    ;After5lass#ubli% void 5losebrowser'6driver&$uit'3C

    C

    Ln e"e%ution %om#letion, you will get bellow given result In %onsole&

    Soft Assertion ?E Bst #agete"t assertion e"e%uted&Soft Assertion ?E @nd #agete"t assertion e"e%uted&Soft Assertion ?E Bst alert assertion e"e%uted&Soft Assertion ?E @nd alert assertion e"e%uted&)ASS!D> wait/and/%li%kFAI+!D> hard/assert/te"t

  • 8/18/2019 Soft Assertion for Selenium WebDriver With TestNG

    4/4

    As #er %onsole result, soft/assert/te"t' method Is e"e%uted full and #rintedall statements even failed @ assertions& Ln other side, hard/assert/te"t'method has not #rinted any statement In %onsole be%ause e"e%ution abortedon rst assertion failure&

    Now If you look at testng test result re#ort, both failures of soft assertion hasbeen re#orted In testng re#ort -ou %an view this #ost to see (LW TL *I!W T!STNG T!ST 2!S1+T 2!)L2T'& That means your assertion failure has beenre#orted without test abortion& +ook at bellow give our testng test resultre#ort&

    http://software-testing-tutorials-automation.blogspot.in/2014/03/how-to-create-and-run-first-testng.htmlhttp://software-testing-tutorials-automation.blogspot.in/2014/03/how-to-create-and-run-first-testng.htmlhttp://software-testing-tutorials-automation.blogspot.in/2014/03/how-to-create-and-run-first-testng.htmlhttp://software-testing-tutorials-automation.blogspot.in/2014/03/how-to-create-and-run-first-testng.html