Showing posts with label bài tập selenium. Show all posts
Showing posts with label bài tập selenium. Show all posts

Bài 3 - Kiểm tra xem coupon giảm giá có hoạt động đúng không?

Hôm nay chúng ta sẽ tiếp tục viết testcase tiếp theo.
Bài này chúng ta sẽ kiểm tra xem coupon có hoạt động đúng như mong hay không, trong trường hợp này là 5%.

Hướng giải quyết vấn đề của mình như sau:
vào trang detail sản phẩm, sau đó mua hàng-> nhập mã giảm giá -> lấy số tiền cụ thể được giảm -> so sánh với số tiền thực tế(cái này tự tính).

package demo_thuchanh;


import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestCase1 {
 private WebDriver driver;
   private String url; 
   
 @BeforeTest
 public void setUp() throws Exception {
  System.setProperty("webdriver.chrome.driver","C:\\chrome\\chromedriver.exe");
     driver = new ChromeDriver();
     url = "http://live.guru99.com/";
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     
   }
   
 @Test 
   public void TestCase() throws Exception {
  driver.get(url); 
     
     // 2. Click Mobile menu
     driver.findElement(By.linkText("MOBILE")).click(); 
  // 3. Click vào sản phẩm
     driver.findElement(By.id("product-collection-image-2")).click();
     // 4. thêm sp vào giỏ hàng
     driver.findElement(By.className("add-to-cart-buttons")).click();
     driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
     // 5. áp dụng mã giảm giá
     driver.findElement(By.id("coupon_code")).sendKeys("GURU50");
     driver.findElement(By.cssSelector("[title=\"Apply\"]")).click();
     // 6. lấy số tiền giảm giá (nó hiện là -$25.00) nên sẽ phải chuyển nó qua StringBuilder và cắt vị trí 0 tới 2-> sẽ thành 25.00, 
     //sau đó cắt tiếp từ vị trí 2-5> sẽ được 25
     String giamgia=driver.findElement(By.xpath("//*[@id=\"shopping-cart-totals-table\"]/tbody/tr[2]/td[2]")).getText();
     StringBuilder str=new StringBuilder(giamgia);
     str.delete(0, 2);
     str.delete(2, 5);
     // lấy giá 5% để so sánh với giá trên xem đúng không
     int giagiam=(500*5)/100;
     //so sánh, nếu đúng thì testcase pass, trái lại thì fail
     try {
                //chúng ta cần ép kiểu nó về String để so sánh, vì 1 thằng là int, 1 thằng là StringBuilder, không so sánh được
      Assert.assertEquals(String.valueOf(str), String.valueOf(giagiam)); 
       } catch (Exception e) {
        e.printStackTrace();
       }
   
   }
 
 
 
 @AfterTest
 public void tearDown() throws Exception {
  driver.quit();
   }

}


Sau khi chạy code thì kết quả ok, như mong muốn :D


Trong bài này, chúng ta học được

  • Cách ép kiểu khác như int...về String
  • Cách chuyển String về StringBuilder để dễ dàng cắt ký tự ở vị trí mong muốn
Hẹn gặp các bạn ở bài kế tiếp!




Bài 2: Xác nhận giá của sản phẩm trong list page and details page là bằng nhau

Chào các bạn, ở bài tập trước chúng ta đã học được 1 vài bước cơ bản rồi, hôm nay chúng ta sẽ làm bài số 2.
Vì một số lý do như có quá nhiều thông báo...nên mình sẽ không dùng trang web của adayroi nữa mà chuyển qua web của guru99 để thực hành.
Testcase của bài tập ngày hôm nay như sau:



Bây giờ chúng ta thực hành thôi, bài này mình sẽ sử dụng testNG để các bạn thấy được cấu trúc của nó. Bạn hãy chắc chắn rằng bạn đã cài testNG cho eclipse theo hướng dẫn ở đây

package demo_thuchanh;

import static org.testng.Assert.assertEquals;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestCase1 {
 private WebDriver driver;
   private String url; 
   
 @BeforeTest
 public void setUp() throws Exception {
  System.setProperty("webdriver.chrome.driver","C:\\chrome\\chromedriver.exe");
     driver = new ChromeDriver();
     url = "http://live.guru99.com/";
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
   }
   
   @Test
   public void TestCase() throws Exception {
  
  // 1. Go to http://live.guru99.com
     driver.get(url); 
     
     // 2. Click on Mobile menu
     driver.findElement(By.linkText("MOBILE")).click(); 
   
     // 3. In the list of all mobile , read the cost of Sony Xperia mobile (which is $100)             
     String gia_XPeria = driver.findElement(By.cssSelector("#product-price-1 > span.price")).getText();
    
     // 4. Click on Sony Xperia mobile     
     driver.findElement(By.id("product-collection-image-1")).click();
     
     // 5. Read the XPeria mobile price from details page
     String gia_Detail = driver.findElement(By.cssSelector("span.price")).getText();
          
     //  Product price in list and details page should be equal ($100)
     try {
         assertEquals(gia_XPeria, gia_Detail); 
       } catch (Exception e) {
        e.printStackTrace();
       }
   }
 
 @AfterTest
 public void tearDown() throws Exception {
  driver.quit();
   }

}


Hãy nhìn vào code, bạn sẽ thấy cú pháp @BeforeTest, @Test và @AfterTest.

  • Những thứ setup cần thiết, chúng ta sẽ nhét vào BeforeTest
  • Các bước thực hiện testcase, thì chúng ta sẽ cho vào Test
  • Sau cùng thì sẽ cho vào AfterTest, kiểu như close() hay quit() :D

Đây là kết quả sau khi chạy testcase này

Hẹn gặp các bạn ở bài tiếp theo.

Bài 1: tìm kiếm và sắp xếp kết quả - adayroi

Bài này chúng ta sẽ thực hành 1 vài yêu cầu cơ bản như sau:
Step 1. vào https://www.adayroi.com, in ra title của trang chủ
Step 2. tìm kiếm từ khóa điện thoại
Step 3. chọn sắp xếp theo Bán chạy nhất
Step 4. chụp ảnh màn hình để xem lại kết quả

Dưới đây là code của bài tập này

/* 

https://haibgit.blogspot.com/

Test Steps

Step 1. vào https://www.adayroi.com, in ra title của trang chủ

Step 2. tìm kiếm từ khóa điện thoại

Step 3. chọn sắp xếp theo Bán chạy nhất

Step 4. chụp ảnh màn hình để xem lại kết quả

*/



package BaiTap;



import java.io.File;

import java.io.IOException;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.apache.commons.io.FileUtils;





public class Ngay1 {

 public static void main(String[] args) throws IOException {

  String url = "https://www.adayroi.com";

  WebDriver  driver = new ChromeDriver();

  driver.get(url);

  System.out.println(driver.getTitle());

  //nhập từ khóa điện thoại và ô tìm kiếm

  driver.findElement(By.id("header__main__segment_search__form__input")).sendKeys("điện thoại");

  //sau đó click tìm kiếm

  driver.findElement(By.id("header__main__segment_search__form__submit")).click();

     driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

     //tìm menu dropdown sắp xếp

     driver.findElement(By.id("products_list_order_by_container")).click();

     //click vào bán chạy nhất

     driver.findElement(By.id("a_order_1")).click();

     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

     //chụp ảnh màn hình để xem kết quả

     File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

  String png = ("C:\\Ngay1\\sap xep"  + ".png");

  FileUtils.copyFile(scrFile, new File(png));
            //đóng trình duyệt
  driver.close();

 }



}

Sau đây chúng ta hãy nhìn hình ảnh lưu trong C:\Ngay1


Hẹn gặp các bạn ở các bài tiếp theo!