Skip to content

Instantly share code, notes, and snippets.

View XPathUsingSibling.cs
[TestMethod]
public void FindSalaryByTextTest()
{
_driver.Navigate().GoToUrl("https://ultimateqa.com/simple-html-elements-for-automation/");
Assert.IsTrue(_driver.
FindElement(By.XPath("//h2[contains(text(), 'no id')]/following-sibling::table//td[contains(text(),'$120,000+')]")).
Displayed);
}
View EnabledElements.cs
@Test
public void enabledElements() throws InterruptedException {
driver.get("https://the-internet.herokuapp.com/dynamic_controls");
WebElement textField = driver.findElement(By.xpath("//input[@type='text']"));
Assert.assertTrue(!textField.isEnabled());
driver.findElement(By.xpath("//button[@onclick='swapInput()']")).click();
Thread.sleep(5000);
Assert.assertTrue(textField.isEnabled());
}
View selectedCheckboxTest.cs
@Test
public void checkboxes() {
driver.get("https://the-internet.herokuapp.com/checkboxes");
WebElement firstCheckbox = driver.findElement(By.xpath("//input[1]"));
if (!firstCheckbox.isSelected()) {
firstCheckbox.click();
}
Assert.assertTrue(firstCheckbox.isSelected());
}
View noCredentialsTest.cs
@Test
public void noCredentialsTest() {
WebElement _loginButton = driver.findElement(By.id("login-button"));
_loginButton.click();
WebElement _errorMessage = driver.findElement(By.xpath("//h3[@data-test='error']"));
// Validate that the message is displayed:
Assert.assertTrue(_errorMessage.isDisplayed());
// Validate the message contains the correct text:
Assert.assertTrue(_errorMessage.getText().equals("Epic sadface: Username is required"));
}
View StringContains.cs
@Test
public void stringContains() {
String longString = "This is a long sentence that contains some words";
String shortString = "some words";
Assert.assertTrue(longString.contains(shortString));
}
View StringComparison.cs
@Test
public void compareStrings() {
String firstWord = "COMPARISON";
String secondWord = "comparison";
Assert.assertEquals(firstWord.toLowerCase(), (secondWord.toLowerCase()));
Assert.assertTrue(firstWord.toLowerCase().equals(secondWord.toLowerCase()));
}
View bmiExercise.cs
@Test
public void bmi() {
double weight = 55;
double height = 1.65;
double bmi = weight / (height * height);
Assert.assertTrue(bmi >= 18.5 && bmi <=25.9);
}
View ComparisonExercise.cs
import org.junit.Assert;
import org.junit.Test;
public class Exercises {
@Test
public void comparison() {
double num1 = 10;
double num2 = 5.5;
Assert.assertTrue("The second number is higher than the first.",num1 > num2);
View VariableNames.cs
// Good variable names
private static ChromeDriver driver;
private static ChromeDriver chromeDriver;
Alert _alert = driver.switchTo().alert();
// Bad variable names
private static ChromeDriver chrome_driver;
private static ChromeDriver chrome_driver;
private static ChromeDriver chromedriver;
public static ChromeDriver _chromeDriver;
View MethodNames.cs
// Good Method Names
public void readUserInformation()
public bool isUserLoggedIn()
//Bad Method Names
public void read_User_Information()
public void readuserinformation()
public void userInformation()