测试断言

问题描述 投票:0回答:1

我需要将断言添加到我的测试中。然而,当我尝试这样做时,它会将 org 返回给我。

junit.ComparisonFailure:   
Expected :This is a new note!  
Actual   :

我必须将数据与已创建的记录进行比较,一切是否正常以及是否正确执行

package java_3lab;

import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class lab3 extends TestBase {
    @Test
    public void a_testLogin() {
        app.getNavigationHelper().openHomePage();
        AccountData user = new AccountData(app.getLoginHelper());
        user.login();
    }

    @Test
    public void b_testAddNote() {
        app.getNavigationHelper().openHomePage();
        AccountData user = new AccountData(app.getLoginHelper());
        NoteData note = new NoteData(app.getNoteHelper());
        note.create("Это новая заметка!");

        // Получаем текст последней созданной заметки
        String lastCreatedNoteText = app.getNoteHelper().getLastCreatedNoteText();

        // Проверяем, что текст заметки соответствует ожидаемому
        Assert.assertEquals("Это новая заметка!", lastCreatedNoteText);
    }

    @Test
    public void c_testEditNote() {
        app.getNavigationHelper().openHomePage();
        AccountData user = new AccountData(app.getLoginHelper());
        NoteEdit note = new NoteEdit(app.getNoteEditHelper());
        note.edit("Теперь это исправленная заметка!");
    }
}
package java_3lab;

import org.openqa.selenium.WebDriver;

public class NoteData {
    private NoteHelper noteHelper;

    public NoteData(NoteHelper noteHelper) {
        this.noteHelper = noteHelper;
    }

    public void create(String noteText) {
        noteHelper.create(noteText);
    }

    public String getLastCreatedNoteText() {
        return noteHelper.getLastCreatedNoteText();
    }
}
package java_3lab;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class NoteHelper extends HelperBase {

    public NoteHelper(ApplicationManager app) {
        super(app);
    }

    public String getLastCreatedNoteText() {
        WebElement postWebElement = driver.findElement(By.xpath("//div[@id='page_wall_posts']/div[1]"));
        String postContent = postWebElement.findElement(By.xpath("//div[@class='wall_text']/div/div")).getText();
        return postContent;
    }

    public void create(String noteText) {
        NavigationHelper navigationHelper = app.getNavigationHelper();
        navigationHelper.openHomePage();

        driver.findElement(By.id("DiText")).click();
        driver.findElement(By.id("DiText")).sendKeys(noteText);
        driver.findElement(By.id("btn_save")).click();
    }
}

我尝试过多种方式做到这一点,包括:

public String getLastCreatedNoteText(){
    return driver.findElement(By.id("DiText")).getText();
}
java selenium-webdriver testing
1个回答
0
投票

您尝试过 Thread.sleep(5000) 或 WebDriverWait 吗?还可以尝试使用“xpath”作为 Web 元素而不是“id”。如果有效,请告诉我们。

© www.soinside.com 2019 - 2024. All rights reserved.