如何使Jenkins Testng套件严重失败以触发Post Build,电子邮件通知

时间:2019-02-15 14:44:13

标签: java selenium jenkins webdriver testng

我在Jenkins服务器上安排了以下Testng程序。

它检查两个字符串之一,如果都找不到,则表示API失败,我希望它提醒我。

但是测试失败了,它的失败程度还不足以使詹金斯在帖子构建中生成提醒我的电子邮件。

它根据邮政编码和地址以及字符串“ Great news!”进行财务检查。或“谢谢”(如果都没有找到),则发布后的版本会发出警报。

任何指针都非常感谢。

package Finance_check;

import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Check_Finance_Latest {

    static WebDriver driver;

    @Test
    public void check_finance() throws IOException, InterruptedException {

        // linux
        // System.setProperty("webdriver.chrome.driver",
        // "//usr//lib//chromium-browser//chromedriver");
        // System.setProperty("webdriver.chrome.driver",
        // "/home/user.name/Selenium/chromedriver");
        // driver = new ChromeDriver();

        // Windows
        System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\Chrome Driver\\chromedriver.exe");
        driver = new ChromeDriver();

        driver.manage().deleteAllCookies();

        driver.get("https://www.somesite.com/");

        // driver.manage().window().maximize();

        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.titleContains("CarShop | UK car supermarket | used cars for sale"));

        Thread.sleep(8000);

        driver.findElement(By.xpath("//button[contains(.,'Accept Cookies')]")).click();

        Thread.sleep(8000);

        driver.findElement(By.xpath("//a[contains(@href,'/apply-for-finance')]")).click();

        Thread.sleep(8000);

        driver.findElement(By.xpath("//button[contains(.,'Continue')]")).click();

        Thread.sleep(8000);

        Select title = new Select(driver.findElement(By.xpath("//select[contains(@name,'title')]")));
        title.selectByVisibleText("Dr");

        Thread.sleep(8000);

        WebElement fname = driver.findElement(By.xpath("//input[contains(@id,'firstname')]"));
        fname.sendKeys("Agent");

        Thread.sleep(8000);

        WebElement lname = driver.findElement(By.xpath("//input[contains(@id,'surname')]"));
        lname.sendKeys("Smith");

        Thread.sleep(8000);

        Select day = new Select(driver.findElement(By.xpath("//select[contains(@name,'dateOfBirth_day')]")));
        day.selectByVisibleText("6");

        Thread.sleep(8000);

        Select month = new Select(driver.findElement(By.xpath("//select[contains(@name,'dateOfBirth_month')]")));
        month.selectByVisibleText("January");

        Thread.sleep(8000);

        Select year = new Select(driver.findElement(By.xpath("//select[contains(@name,'dateOfBirth_year')]")));
        year.selectByVisibleText("1972");

        Thread.sleep(8000);

        WebElement phone = driver.findElement(By.xpath("//input[contains(@type,'tel')]"));
        phone.sendKeys("0207 485769");

        Thread.sleep(8000);

        WebElement email = driver.findElement(By.xpath("//input[contains(@id,'emailAddress')]"));
        email.sendKeys("ITTestingDonotContact@example.com");

        Thread.sleep(8000);

        System.out.println("Filled in name / age details");
        driver.findElement(By.xpath("//button[contains(.,'Continue')]")).click();

        WebElement pcode = driver.findElement(By.xpath("//input[contains(@id,'postCode')]"));
        pcode.sendKeys("W1A 2HG");

        Thread.sleep(8000);

        driver.findElement(By.xpath("//button[contains(.,'Find Address')]")).click();

        Thread.sleep(8000);

        Select address = new Select(driver.findElement(By.xpath("//select[contains(@ng-model,'addressKey')]")));

        Thread.sleep(8000);

        address.selectByVisibleText("7 The Road, LONDON");

        Thread.sleep(8000);

        driver.findElement(By.xpath("//button[contains(.,'Complete Pre-Application')]")).click();

        Thread.sleep(8000);

        try {
            String high = driver.findElement(By.xpath("//h4[contains(.,'Great news!')]")).getText();
            System.out.println("high string found");
        } catch (Exception e) {
        }

        try {
            String low = driver.findElement(By.xpath("//h4[contains(.,'Thank you.')]")).getText();
            System.out.println("low string found");
        } catch (Exception e) {
        }

        String credit_rating = driver.findElement(By.xpath("//h4")).getText();
        System.out.println("Credit Rating Result = " + credit_rating);

        if ("Great news!".equals(credit_rating)) {

            System.out.println("High Credit Found");

        } else if ("Thank you".equals(credit_rating)) {

            System.out.println("Low Credit Found");

        } else {
            System.out.println("Neither String found");

            try {
                throw new Exception("wheres my fiance message ? - fail!");

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

                driver.quit();
            }
        }
    }

    @AfterTest
    public void tearDown() {
        driver.quit();
    }

}

1 个答案:

答案 0 :(得分:1)

您应该使用TestNG声明(http://static.javadoc.io/org.testng/testng/6.11/index.html?org/testng/Assert.html)进行测试。

代替throw new Exception("wheres my fiance message ? - fail!");
您可以使用fail("wheres my fiance message ? - fail!")

这将告诉TestNG您的测试已正确失败。