Selenium+TestNG реальные примеры

Selenium+TestNG реальные примеры

Подскажите как будет выглядеть данный код Junit переконвертированный в TestNG -

package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;

public class test1 {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
        driver =  new InternetExplorerDriver();
        baseUrl = "http://server3/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testUntitled() throws Exception {
        driver.get(baseUrl + "/BOSS/Login.aspx?browser=any ");
        driver.get(baseUrl + "/BOSS/Login.aspx?browser=any ");
        new Select(driver.findElement(By.id("ctl00_cphM_lgnBoss_ddlDts"))).selectByVisibleText("Tas_ap");
        driver.findElement(By.id("ctl00_cphM_lgnBoss_UserName")).clear();
        driver.findElement(By.id("ctl00_cphM_lgnBoss_UserName")).sendKeys("369532");
        driver.findElement(By.id("ctl00_cphM_lgnBoss_Password")).clear();
        driver.findElement(By.id("ctl00_cphM_lgnBoss_Password")).sendKeys("12345");
        driver.findElement(By.id("ctl00_cphM_lgnBoss_btLgn")).sendKeys(" ");
        driver.findElement(By.xpath("//*[@id='ctl00_mnuMainn1']/table/tbody/tr/td/a")).click();
        driver.findElement(By.xpath("//*[@id='ctl00_mnuMainn3']/table/tbody/tr/td/a")).click();
        driver.findElement(By.xpath("//*[@id='ctl00_mnuMainn4']/table/tbody/tr/td/a")).click();
        driver.findElement(By.xpath("//*[@id='ctl00_mnuMainn5']/table/tbody/tr/td/a")).click();
        driver.findElement(By.xpath("//*[@id='ctl00_mnuMainn6']/table/tbody/tr/td[1]/a")).click();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }
}



Так же, если у кокго есть примеры тестов написанных на TestNG дайте пожалуста ознакомиться.

 

да особо ваш код не поменяется, просто будут импортированны другие библиотеки и использованны другине аннотации

 

import java.util.concurrent.TimeUnit;
 
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
 
 
public class test1 {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    
    @BeforeMethod
    public void setUp() throws Exception {
        driver =  new InternetExplorerDriver();
        baseUrl = "http://server3/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
    
    @Test
    public void testUntitled() throws Exception {
        driver.get(baseUrl + "/BOSS/Login.aspx?browser=any ");
        driver.get(baseUrl + "/BOSS/Login.aspx?browser=any ");
        new Select(driver.findElement(By.id("ctl00_cphM_lgnBoss_ddlDts"))).selectByVisibleText("Tas_ap");
        driver.findElement(By.id("ctl00_cphM_lgnBoss_UserName")).clear();
        driver.findElement(By.id("ctl00_cphM_lgnBoss_UserName")).sendKeys("369532");
        driver.findElement(By.id("ctl00_cphM_lgnBoss_Password")).clear();
        driver.findElement(By.id("ctl00_cphM_lgnBoss_Password")).sendKeys("12345");
        driver.findElement(By.id("ctl00_cphM_lgnBoss_btLgn")).sendKeys(" ");
        driver.findElement(By.xpath("//*[@id='ctl00_mnuMainn1']/table/tbody/tr/td/a")).click();
        driver.findElement(By.xpath("//*[@id='ctl00_mnuMainn3']/table/tbody/tr/td/a")).click();
        driver.findElement(By.xpath("//*[@id='ctl00_mnuMainn4']/table/tbody/tr/td/a")).click();
        driver.findElement(By.xpath("//*[@id='ctl00_mnuMainn5']/table/tbody/tr/td/a")).click();
        driver.findElement(By.xpath("//*[@id='ctl00_mnuMainn6']/table/tbody/tr/td[1]/a")).click();
    }
 
    @AfterMethod
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }
 
    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }
}

Собственно вопрос возник оказывается не зря, мой пример выше валится после логина, а вот ваш переделанный в тестНГ работает.
Вопрос почему? и стоит ли сразу писать в тестНГ а про джи юнит забыть?

P.S. - и как для этого примера можно сделать задержку в секунду после каждого шага?

Не дейлайте ваш скрипт медленным, испoльзуйте Implicit wait

ну надо сначала понять в чем проблема, что бы ее чинить

на какой точке у вас падает ваш тест?

касательно JUnit или TestNG, и тот и другой фреймворк успешно применяется в работе

задержку можно посмавить, чтобы посмотреть что проблемы в синхронизации

и если это так, то тогда поставить явные ожидания на конкретные элементы.