0

I try to get value (email adress) from page https://temp-mail.org/ , and put it to the string.

So my code is this:

maven dependencies:

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>

    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>4.3.1</version>
    </dependency>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.8</version>
        <scope>test</scope>
    </dependency>
</dependencies>

I create Abstract page

public abstract class AbstractPage {
    protected WebDriver driver;

    protected AbstractPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }
    protected abstract AbstractPage openPage(String url);
}

TempMailPage:

public class TempMailOrgPage extends AbstractPage {
    String regex = "^[A-Za-z0-9+_.-]+@(.+)$";
    Pattern pattern = Pattern.compile(regex);
    
    public TempMailOrgPage(WebDriver driver) {
        super(driver);
    }
    
    public TempMailOrgPage openPage(String url) {
        driver.get(url);
        driver.manage().window().maximize();
        return this;
    }
    @FindBy(xpath = "//input[@id='mail']")
    WebElement mailField;

    public TempMailOrgPage getAddress() {
        //new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElement(mailField, regex));
        new WebDriverWait(driver, 10).until(ExpectedConditions.textMatches((By) mailField, pattern));
        System.out.println(mailField.getAttribute("value"));
        return this;
    }
}

and Test

public class MainTest {
    WebDriver driver;
    
    @BeforeMethod(alwaysRun = true)
    public void browserRun() {
        WebDriverManager.firefoxdriver().setup();
        driver = new FirefoxDriver();
    }
    @Test(description = "try get addresb")
    public void scenarioWithCopyingEmail()  {
        new TempMailOrgPage(this.driver)
                .openPage("https://temp-mail.org/")
                .getAddress();
    }
    
    @AfterMethod(alwaysRun = true)
    public void afterTestCompleted() {
        driver.quit();
        driver = null;
    }
}

And after test running I get error. I understand that a mistake in waiting then email address is generating.

And instead of a word "Loading" will be mail address. In this line I try to use pattern:

 new WebDriverWait(driver, 10).until(ExpectedConditions.textMatches((By) mailField, pattern));

What am I doing wrong with waiting?

P.s. using thread.sleep not a way.

7
  • mailField is representing input field that does not have any attribute value as far as I see.Not sure if you are looking for data-value Commented Mar 13, 2021 at 9:55
  • Why method openPage() in class TempMailOrgPage doesn't do anything? Shouldn't it open the URL with driver.get()? Commented Mar 13, 2021 at 9:56
  • @Fenio : it would do "this.driver = driver; PageFactory.initElements(driver, this);". See there is this and super involved. Commented Mar 13, 2021 at 9:58
  • @cruisepandey No, that's what constructor does. I'm asking about openPage() method which only returns reference to the current object... Where is the line of code that actually opens temp-mail.org? Commented Mar 13, 2021 at 9:59
  • @Fenio : Yes I agree, driver.get("") is missing in code. Commented Mar 13, 2021 at 10:06

1 Answer 1

1

Problem

I think the problem is that web element [mailField] don't actually have a text variable.

I try the following code:

driver.get("https://temp-mail.org");
Thread.sleep(60000);
System.out.println(driver.findElementById("mail").getText());

waiting for 60 second and although the ui display the email. The system still print out blank element. The method [textMatches] will get the text of the web element to compare with the regrex. When the web element don't have text or text is blank, how it suppose to compare. So of course it will always throw out time out exepception.

Solution

I can not find other method which work actually like [textMatches] and able to apply to [getAttribue("value")]. So you can concern this solution as a work arround. Waiting for attribute value contain [@].

new WebDriverWait(driver, 20)
    .until(ExpectedConditions
    .attributeContains(driver.findElementById("mail"), "value", "@"));

System.out.println(driver.findElementById("mail").getAttribute("value"));

Working pretty well when i tested

[email protected]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.