Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

webdriver - java.lang.AssertionError: Maps do not match for key:country actual:Mgdtoemizqbpfrt expected:

Еhe actual does not match the expected, although it should. I'm a newbie. I am writing a Junit test using selenium methods. I wrote a successful test for generating random data and entering it into the new customer registration fields on the test site. Unfortunately, when comparing two Maps, I ran into a problem. I would be glad if someone explains what the problem is. Thank you.

import org.apache.commons.lang3.RandomStringUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class Client {
    WebDriver driver;
    String email;
    String country;
    String city;
    String phoneNumber;
    String skype;

    private static String PAGE_URL = "http://serviceacc.foxminded.com.ua/";

    @FindBy(xpath = "//input[@id='username']")
    WebElement usernameField;

    @FindBy(xpath = "//input[@id='password']")
    WebElement passwordField;

    @FindBy(xpath = "//input[@name='login-submit']")
    WebElement submitButton;

    @FindBy(xpath = "//span[contains(.,'Клиенты')]")
    WebElement customerPage;

    @FindBy(xpath = "//button[contains(.,'Create Client')]")
    WebElement createClientButton;

    @FindBy(xpath = "//input[@name='firstName']")
    WebElement firstnameField;

    @FindBy(xpath = "//input[@name='lastName']")
    WebElement lastnameField;

    @FindBy(xpath = "//button[@type='submit']")
    WebElement submitClientButton;

    @FindBy(xpath = "//input[@placeholder='Поиск']")
    WebElement searchField;

    @FindBy(xpath = "//a[contains(.,'Ivan Ivanov')]")
    WebElement clientWasGenerated;

    @FindBy(xpath = "//input[@name='email']")
    WebElement clientEmail;

    @FindBy(xpath = "//input[@name='country']")
    WebElement clientCountry;

    @FindBy(xpath = "//input[@name='city']")
    WebElement clientCity;

    @FindBy(xpath = "//input[@name='phoneNumber']")
    WebElement clientPhoneNumber;

    @FindBy(xpath = "//input[@name='skype']")
    WebElement clientSkype;

    @FindBy(xpath = "//button[@type='submit'][contains(.,'Save')]")
    WebElement saveAllData;

    public Client(WebDriver driver) {
        this.driver = driver;
        driver.get(PAGE_URL);
        PageFactory.initElements(driver, this);
    }
    public void authorization() {
        usernameField.sendKeys("admin");
        passwordField.sendKeys("admin");
        submitButton.click();
    }
    public void locateForCreation() {
        customerPage.click();
        createClientButton.click();
    }
    public void setClientName() {
        firstnameField.sendKeys("Ivan");
        lastnameField.sendKeys("Ivanov");
        submitClientButton.click();
    }
    public void setClientEmail(){
        String randomEmail = RandomStringUtils.randomAlphabetic(10);
        email = randomEmail+"@"+randomEmail+".com";
        clientEmail.sendKeys(email);
    }
    public void setClientCountry(){
        String randomCountry = RandomStringUtils.randomAlphabetic(15).toLowerCase();
        country = randomCountry.substring(0,1).toUpperCase()+randomCountry.substring(1);
        clientCountry.sendKeys(country);
    }
    public void setClientCity(){
        String randomCity = RandomStringUtils.randomAlphabetic(15).toLowerCase();
        city = randomCity.substring(0,1).toUpperCase()+randomCity.substring(1);
        clientCity.sendKeys(city);
    }
    public void setClientPhoneNumber(){
        phoneNumber = RandomStringUtils.randomNumeric(10);
        clientPhoneNumber.sendKeys(phoneNumber);
    }
    public void setClientSkype() {
        skype = RandomStringUtils.randomAlphanumeric(15);
        clientSkype.sendKeys(skype);
    }
    public void clickOnSave(){

        saveAllData.click();
     }
    public void openNewGeneretedClient() {
    searchField.sendKeys("Ivan Ivanov");
    clientWasGenerated.click();
    }
    public void compareData() throws InterruptedException {

        driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);
        Thread.sleep(3000);

        Map<String, String> mapActual = new HashMap<>();
        Map<String, String> mapExpected = new HashMap<>();

        mapActual.put("e_mail", email);
        mapActual.put("country", country);
        mapActual.put("city", city);
        mapActual.put("phoneNumber", phoneNumber);
        mapActual.put("skype", skype);

        mapExpected.put("e_mail",clientEmail.getText());
        mapExpected.put("country", clientCountry.getText());
        mapExpected.put("city", clientCity.getText());
        mapExpected.put("phone number", clientPhoneNumber.getText());
        mapExpected.put("skype", clientSkype.getText());

        Assert.assertEquals(mapActual,mapExpected);
    }
}

ClientTest

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

public class ClientTest {
    WebDriver driver;

    @Before
    public void setup() {
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test
    public void applyAsClient() throws InterruptedException {
        Client client = new Client(driver);
        client.authorization();

        client.locateForCreation();
        client.setClientName();

        client.setClientEmail();
        client.setClientCountry();
        client.setClientCity();
        client.setClientPhoneNumber();
        client.setClientSkype();
        client.clickOnSave();

        client.openNewGeneretedClient();
        client.compareData();
    }
    @After
    public void close(){

        driver.close();
    }
}

Was writen at Intellij


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reason was not found out, but a solution was found. I used getAttribute("value") instead of getText().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...