# How to avoid random Pages, Screen, Modal in Java?

**URL:** <https://discuss.appium.io/t/how-to-avoid-random-pages-screen-modal-in-java/35618>\
**Category:** Support\
**Created:** [February 8, 2022, 6:59pm UTC](https://discuss.appium.io/t/how-to-avoid-random-pages-screen-modal-in-java/35618 "2022-02-08T18:59:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felipe\_Rossi](https://discuss.appium.io/user_avatar/discuss.appium.io/felipe_rossi/32/9728_2.png) [@Felipe\_Rossi](https://discuss.appium.io/u/Felipe_Rossi)\
**Post date:** [February 8, 2022, 6:59pm UTC](https://discuss.appium.io/t/how-to-avoid-random-pages-screen-modal-in-java/35618/1 "2022-02-08T18:59:58Z")

</div>

When i run my code everytime pass by login, after fill every field and click on the button, my code validates if the word: “Descubra” of page “Descubra” is displyed:

//Esperando a página: “Descubra Carregar”  
WebDriverWait aguardarCarregarPaginaDescubra = new WebDriverWait(driver, 20);  
aguardarCarregarPaginaDescubra.until(ExpectedConditions.visibilityOfElementLocated(MobileBy.id(“br.com.vivo:id/toolbar\_layout”)));

//Pegando o texto do Descubra  
String textoDescubra = driver.findElementById(“br.com.vivo:id/toolbar\_layout”).getAttribute(“content-desc”);

//Comparando se o Texto Exibido é o Descubra para validar se o login foi realizado com sucesso  
Assert.assertEquals(“Descubra”, textoDescubra);

This page is what my code validates

 ![fazerLoginViaCPF%7Bsenha%3D106338%2C%20cpf%3D22270969855%7D08-02-2022%2015-42-38](https://discuss.appium.io/uploads/default/original/2X/3/3161d63309767185c9311e9e3028526cff49f62c.jpeg)

although, sometimes is displayed a random page,random screen, random modal, and always the validation returns with error, how i can avoid this kind of stuff ?

This is the random page, random modal that is displayed sometimes

 ![fazerLoginViaCPF%7Bsenha%3D106338%2C%20cpf%3D34587596892%7D08-02-2022%2015-41-55](https://discuss.appium.io/uploads/default/original/2X/e/eff766bfd641a1c7c546937f037fdd227f7c338e.jpeg)

---

<div class="post-metadata">

**Author:** ![wreed](https://discuss.appium.io/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wreed](https://discuss.appium.io/u/wreed)\
**Post date:** [February 8, 2022, 11:05pm UTC](https://discuss.appium.io/t/how-to-avoid-random-pages-screen-modal-in-java/35618/2 "2022-02-08T23:05:22Z")

</div>

Here are the three things I can see you doing here:

1. Ask developers not to include modal screen in debug app
2. Write your code with a decision tree (if/elsif/else, or switch) that will allow you to handle the modal in code
3. Write your code with a Screen Factory, and the instead of writing the tests to instantiate a particular screen, you just ask the Screen Factory to return the current screen and then have your code handle that particular screen.

Option 3 is what we do in our code. No idea what language you use so here’s a generic definition:

[https://en.wikipedia.org/wiki/Factory\_method\_pattern](https://en.wikipedia.org/wiki/Factory_method_pattern)

---

<div class="post-metadata">

**Author:** ![Felipe\_Rossi](https://discuss.appium.io/user_avatar/discuss.appium.io/felipe_rossi/32/9728_2.png) [@Felipe\_Rossi](https://discuss.appium.io/u/Felipe_Rossi)\
**Post date:** [February 9, 2022, 7:34pm UTC](https://discuss.appium.io/t/how-to-avoid-random-pages-screen-modal-in-java/35618/3 "2022-02-09T19:34:31Z")

</div>

Thanks, i will try these option
