-3

I am a QA Engineer writing a test script. I am attempting to set a URL string variable that is equal to the URL of the current page in a method and call the method containing the variable in a test method.

*Edit**** Originally I thought this was an issue with the variable itself but this is not the case. I have adjusted my question to better represent the problem

When I initiate and define the URL variable in the same class and method as the test it works fine

public void RFPsCreatedStatusTest(){
    RequestsataGlance.RFPinProgressbtnclick();
    RequestsataGlance.CreatedClick();
    String url = driver.getCurrentUrl();
   System.out.println(url);
Assert.assertTrue(url.contains("url.com"), "Incorrect Page, SR list call status does not match status selected ");

But when I create and call the public void() {} URL method [createdassertion]separately from outside of the test method and class it doesn't work. Thoughts?

 @Test
        public void RFPsCreatedStatusTest(){
            RequestsataGlance.RFPinProgressbtnclick();
            RequestsataGlance.CreatedClick();
            RequestsataGlance.Createdassertion();


        }`
Lamar
  • 63
  • 8

2 Answers2

2

the line below is useless, as you will redefine url variable in next statement

url = new String();

after the line

url = driver.getCurrentUrl();

url will contain the browser url, then why should it contain the string "[expected result]"? of course it will not, as it will contain an URL

EDIT 1

from definition

assertTrue(java.lang.String message, boolean condition) 

so use it as below

Assert.assertTrue("Incorrect Page, SR list call status does not match status selected ", url.contains("whatever.com"));
ddb
  • 2,423
  • 7
  • 28
  • 38
  • My apologies for the confusion I know that it will return whatever the URL is but since there wasn't one I just put [expected condition] as poor choice for a placeholder. – Lamar Jul 06 '16 at 15:22
  • then, maybe the `driver.getCurrentUrl();` statement does not return anything, that is it returns null and so you can't use the url variable – ddb Jul 06 '16 at 15:24
  • Good thought, I defined the URL to be "123" , the assertion should have failed but instead I still got the exception. – Lamar Jul 06 '16 at 15:34
  • @Lamar, I updated my answer, did you try it again? You should use the assert differently as I wrote – ddb Jul 06 '16 at 20:04
  • I double checked the assert statement and it is boolean, string: not string, boolean. I realized I left out an important piece and forgive as I am just getting used to SO I am calling that Method into a test method in another class, see my updated post – Lamar Jul 07 '16 at 01:32
  • see the aboveabove... – Lamar Jul 07 '16 at 01:40
1

The only way anything can be null in your code is if

driver.getCurrentUrl()

returns null. After all, you wrote a Test and found a problem. Just fix it! ;-)

Btw.: your code will be much more easy to read if you simply write:

String url = driver.getCurrentUrl();
martinhh
  • 336
  • 2
  • 10