M
M
Maxim Isaev2021-03-19 22:17:56
Java
Maxim Isaev, 2021-03-19 22:17:56

How to avoid repetition in tests?

Please tell me how to avoid repeated opening of the MainPage page in tests, as here, for example:

@Test
    public void IsTheActualTitleEqualledToTheExpectedTitle() {
        String command = "git config --global user.name  \"New Sheriff in Town\"" +
                "\ngit reset $(git commit-tree HEAD^{tree} -m \"Legacy code\")" +
                "\ngit push origin master --force";

        MainPage mainPage = new MainPage(driver)
                .openPage()
                .clickNewPasteBtn()
                .inputTextArea(command)
                .selectPastSyntaxHighlightingFromDropDownList("Bash")
                .selectPasteExpirationFromDropDownList("10 Minutes")
                .inputTitle("how to gain dominance among developers")
                .clickCreateNewPasteBtn();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class = 'info-top']/h1")));
        String actualResult = driver.findElement(By.xpath("//div[@class = 'info-top']/h1")).getText();
        Assert.assertEquals(actualResult, "how to gain dominance among developers");
    }

    @Test
    public void IsTheActualSyntaxEqualledToTheExpectedSyntax() {
        String command = "git config --global user.name  \"New Sheriff in Town\"" +
                "\ngit reset $(git commit-tree HEAD^{tree} -m \"Legacy code\")" +
                "\ngit push origin master --force";

        MainPage mainPage = new MainPage(driver)
                .openPage()
                .clickNewPasteBtn()
                .inputTextArea(command)
                .selectPastSyntaxHighlightingFromDropDownList("Bash")
                .selectPasteExpirationFromDropDownList("10 Minutes")
                .inputTitle("how to gain dominance among developers")
                .clickCreateNewPasteBtn();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text() = 'Bash']")));
        String actualResult = driver.findElement(By.xpath("//a[text() = 'Bash']")).getText();
        Assert.assertEquals(actualResult, "Bash");
    }

    @Test
    public void IsTheActualCommandsEqalledToTheExpectedCommands() {
        String command = "git config --global user.name  \"New Sheriff in Town\"" +
                "\ngit reset $(git commit-tree HEAD^{tree} -m \"Legacy code\")" +
                "\ngit push origin master --force";

        MainPage mainPage = new MainPage(driver)
                .openPage()
                .clickNewPasteBtn()
                .inputTextArea(command)
                .selectPastSyntaxHighlightingFromDropDownList("Bash")
                .selectPasteExpirationFromDropDownList("10 Minutes")
                .inputTitle("how to gain dominance among developers")
                .clickCreateNewPasteBtn();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ol[@class='bash']")));
        String actualResult = driver.findElement(By.xpath("//ol[@class='bash']")).getText();
        Assert.assertEquals(command, actualResult);
    }


Thanks for the hints.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasvaller, 2021-03-20
@MaximIs

In the class, create the field MainPage mainPage.
In the new annotated method @Before, initialize your mainPage. That. before tests, mainPage will be initialized first and you can use it in all tests.

A
Andrey Baranov, 2021-04-05
@ApoFis_93

agree with Vasvaller

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question