# Stop a test case if an element A appears, if not, continue the test case

**URL:** <https://forum.robotframework.org/t/stop-a-test-case-if-an-element-a-appears-if-not-continue-the-test-case/6546>\
**Category:** Robot Framework\
**Created:** [5 December 2023 18:45 UTC](https://forum.robotframework.org/t/stop-a-test-case-if-an-element-a-appears-if-not-continue-the-test-case/6546 "2023-12-05T18:45:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinayrana](https://avatars.discourse-cdn.com/v4/letter/v/7ab992/32.png) [@vinayrana](https://forum.robotframework.org/u/vinayrana)\
**Post date:** [5 December 2023 18:45 UTC](https://forum.robotframework.org/t/stop-a-test-case-if-an-element-a-appears-if-not-continue-the-test-case/6546/1 "2023-12-05T18:45:09Z")

</div>

Hi All,

I’m trying to implement a code where during a test case,  
IF an Element A appears then stop/fail the test case, if not continue with the test case.

i’m using below code. however, it doens’t seem to work.

${textPresence} Wait Until Page Contains Element ‘id’

Run Keyword If ‘${textPresence}’ == ‘True’ Wait Until Page Contains ‘text’ timeout=2700s

Any help is appreciated.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [5 December 2023 23:33 UTC](https://forum.robotframework.org/t/stop-a-test-case-if-an-element-a-appears-if-not-continue-the-test-case/6546/2 "2023-12-05T23:33:17Z")

</div>

Hi Vinay,

Many libraries have some form of a `should not` keyword (e.g. [Element Should Not Be Visible](https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Element%20Should%20Not%20Be%20Visible) from SeleniumLibrary)

If the library you’re using doesn’t have that then you can use the BuiltIn [Run Keyword And Expect Error](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Run%20Keyword%20And%20Expect%20Error) to inverse the logic of any keyword you have.

Hope that helps,

Dave.

---

<div class="post-metadata">

**Author:** ![vinayrana](https://avatars.discourse-cdn.com/v4/letter/v/7ab992/32.png) [@vinayrana](https://forum.robotframework.org/u/vinayrana)\
**Post date:** [6 December 2023 20:05 UTC](https://forum.robotframework.org/t/stop-a-test-case-if-an-element-a-appears-if-not-continue-the-test-case/6546/3 "2023-12-06T20:05:49Z")

</div>

Hey Dave,  
Thanks for the reply.  
I’m using selenium library and have those keywords.  
However, in my case the process loads from 1% to 100%. It always doesn’t complete 100 %. sometimes it fails at 4% or 8% and an error is displayed. so I need the test case to fail if an error is displayed within a certain time frame. If not, then conutinue with the test case until it reaches 100%.

to give you clear picture. without the error element i’m using i had below code which work fine as long as the page loads up to 100%

Wait Until Page Contains 100% timeout=3000s

but i want to add a code before this which checks if an error is displayed on the page, then fail/stop the test case.

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [7 December 2023 00:44 UTC](https://forum.robotframework.org/t/stop-a-test-case-if-an-element-a-appears-if-not-continue-the-test-case/6546/4 "2023-12-07T00:44:31Z")

</div>

Hi Vinay,

Then use `Run Keyword And Expect Error` as I mentioned like this:

first you need the locator for the error message, replace `${ErrorMessage}` in my example with the real locator

```python
Run Keyword And Expect Error Wait Until Page Contains ${ErrorMessage} timeout=1500s
Wait Until Page Contains 100% timeout=3000s

```

- If the error message shows on the screen within the timeout the first `Wait Until Page Contains` will pass, but as we expect it to fail `Run Keyword And Expect Error` will then fail and the test will stop

- If the error message doesn’t show on the screen within the timeout the first `Wait Until Page Contains` will fail, but as we expected it to fail `Run Keyword And Expect Error` will then pass and continue to the second `Wait Until Page Contains`

Dave.

---

<div class="post-metadata">

**Author:** ![vinayrana](https://avatars.discourse-cdn.com/v4/letter/v/7ab992/32.png) [@vinayrana](https://forum.robotframework.org/u/vinayrana)\
**Post date:** [7 December 2023 17:29 UTC](https://forum.robotframework.org/t/stop-a-test-case-if-an-element-a-appears-if-not-continue-the-test-case/6546/5 "2023-12-07T17:29:08Z")

</div>

Hey Dave,  
I tried to implement the first code ‘run keyword and expect error’. however, when i do that it’s not allowing me to add ‘wait until page contains’ as you mentioned above.

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/4/439abc4414465eb6fad8fd1a5a9f8dad654db6f9.png)

instead it is asking for below.

![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/b/b9052be6892b66f4b9355e4f7a8be3462ba8927c.png)

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [7 December 2023 23:10 UTC](https://forum.robotframework.org/t/stop-a-test-case-if-an-element-a-appears-if-not-continue-the-test-case/6546/6 "2023-12-07T23:10:44Z")

</div>

Hi Vinay,

Yeah I missed that, just use a `*` to match any error

```python
Run Keyword And Expect Error * Wait Until Page Contains ${ErrorMessage} timeout=1500s
Wait Until Page Contains 100% timeout=3000s

```

Unless you want to restrict it to a specific error.

Dave.
