Welcome to My Portfolio
1// BDD (Cucumber) - Cypress Test
2import { Given, When, Then } from '@badeball/cypress-cucumber-preprocessor';
3
4Given('I open the login page', () => {
5 cy.visit('https://example.com/login');
6});
7
8When('I enter valid credentials', () => {
9 cy.get('#username').type('testuser');
10 cy.get('#password').type('password123');
11});
12
13When('I click the login button', () => {
14 cy.get('#login-button').click();
15});
16
17Then('I should be redirected to the dashboard', () => {
18 cy.url().should('include', '/dashboard');
19});
20
21// Mock API response using Cypress Intercept (login.js)
22describe('Mock API Login Test', () => {
23 it('should mock login API response', () => {
24 cy.intercept('POST', '/api/login', {
25 statusCode: 200,
26 body: { token: 'mocked_token' }
27 }).as('mockLogin');
28
29 cy.visit('https://example.com/login');
30 cy.get('#username').type('testuser');
31 cy.get('#password').type('password123');
32 cy.get('#login-button').click();
33 cy.wait('@mockLogin');
34 cy.url().should('include', '/dashboard');
35 });
36});
1# BDD - Robot Framework Test (login.robot)
2*** Settings ***
3Library SeleniumLibrary
4Library Collections
5Library RequestsLibrary
6
7*** Variables ***
8{LOGIN_URL} https://example.com/login
9{API_URL} https://example.com/api/login
10{BROWSER} Chrome
11{USERNAME} testuser
12{PASSWORD} password123
13
14*** Test Cases ***
15Valid Login
16 Open Browser {LOGIN_URL} {BROWSER}
17 Input Text id=username {USERNAME}
18 Input Text id=password {PASSWORD}
19 Click Button id=login-button
20 Wait Until Location Contains /dashboard
21 Close Browser
22
23Mock API Login
24 Create Session mysession {API_URL}
25 {response} POST On Session mysession /login json={'username': '{USERNAME}', 'password': '{PASSWORD}'}
26 Should Be Equal As Strings {response.status_code} 200
27 Log To Console {response.json()}
28
1// TDD - Java Selenium with JUnit
2import org.junit.After;
3import org.junit.Before;
4import org.junit.Test;
5import org.openqa.selenium.By;
6import org.openqa.selenium.WebDriver;
7import org.openqa.selenium.WebElement;
8import org.openqa.selenium.chrome.ChromeDriver;
9
10import static org.junit.Assert.assertTrue;
11
12public class LoginTest {
13 private WebDriver driver;
14
15 @Before
16 public void setUp() {
17 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
18 driver = new ChromeDriver();
19 driver.get("https://example.com/login");
20 }
21
22 @Test
23 public void testValidLogin() {
24 WebElement username = driver.findElement(By.id("username"));
25 WebElement password = driver.findElement(By.id("password"));
26 WebElement loginButton = driver.findElement(By.id("login-button"));
27
28 username.sendKeys("testuser");
29 password.sendKeys("password123");
30 loginButton.click();
31
32 assertTrue(driver.getCurrentUrl().contains("/dashboard"));
33 }
34
35 @After
36 public void tearDown() {
37 driver.quit();
38 }
39}