簡體   English   中英

如何使 Jenkins Testng 套件嚴重失敗以觸發 Post Build、電子郵件通知

[英]How to make a Jenkins Testng suite fail badly to trigger Post Build , email notification

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

它檢查兩個字符串之一,如果都沒有找到,這意味着 API 失敗,我希望它提醒我。

但是測試失敗了,對於 Jenkins 在后期構建中生成電子郵件來提醒我來說,它並沒有失敗得足夠嚴重。

它根據郵政編碼和地址進行財務檢查,如果字符串“好消息!” 或者“謝謝”,如果沒有發現它失敗,那么發布構建將發出警報。

任何指針都非常感謝。

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();
    }

}

您應該使用 TestNG Assert ( 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 您的測試已正確失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM