The scrollbar should be enabled only when necessary.
Language: Java
Framework: Selenium 4
1// Assumption: the webpage under test has a scrollbar 2// defined using CSS34import org.openqa.selenium.By;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.chrome.ChromeDriver;8import org.openqa.selenium.JavascriptExecutor;910public class ScrollbarFunctionalityTest {11 12 public static void main(String[] args) {13 14 // local driver setup15 System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");16 WebDriver driver = new ChromeDriver();17 JavascriptExecutor js = (JavascriptExecutor) driver;18 19 // remote driver setup with desired capabilities20 // DesiredCapabilities capabilities = DesiredCapabilities.chrome();21 // ChromeOptions options = new ChromeOptions();22 // options.addArguments("--disable-infobars");23 // capabilities.setBrowserName("chrome");24 // capabilities.setPlatform(Platform.LINUX);25 // capabilities.setCapability(ChromeOptions.CAPABILITY, options);26 // WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);27 28 // navigate to webpage29 driver.get("http://www.example.com");3031 // find page content and viewport32 WebElement pageContent = driver.findElement(By.tagName("html"));33 WebElement viewport = driver.findElement(By.tagName("body"));34 35 // get scroll height and viewport height36 long pageHeight = (long) js.executeScript("return arguments[0].scrollHeight", pageContent);37 long viewportHeight = (long) js.executeScript("return arguments[0].clientHeight", viewport);38 39 // check if scrollbar is enabled or disabled40 if (pageHeight > viewportHeight) {41 System.out.println("Scrollbar is enabled");42 } else {43 System.out.println("Scrollbar is disabled");44 }45 46 // close the browser47 driver.quit();48 }49}
Language: Python
Framework: Selenium 4
1# Assumptions:2# - The webpage is fully loaded3# - The page has content that requires scrolling45# Importing necessary packages6from selenium.webdriver import Chrome7from selenium.webdriver.common.keys import Keys89# Creating a Chrome webdriver instance10driver = Chrome()1112# Opening the webpage13driver.get("https://www.example.com")1415# Adding remote client with desired capabilities16'''17from selenium.webdriver.common.desired_capabilities import DesiredCapabilities18capabilities = DesiredCapabilities.CHROME.copy()19capabilities['version'] = '73.0.3683.68'20capabilities['enableVNC'] = True21capabilities['enableVideo'] = False22driver = Chrome(desired_capabilities=capabilities)23'''2425# Finding the height of the page using JavaScript26page_height = driver.execute_script("return document.body.scrollHeight")2728# Finding the height of the viewport using JavaScript29viewport_height = driver.execute_script("return window.innerHeight")3031# Verifying if the scrollbar is enabled when necessary32if page_height > viewport_height:33 scrollbar_enabled = True34else:35 scrollbar_enabled = False3637# Closing the webdriver instance38driver.quit()
Language: Javascript
Framework: Cypress
1//Assumptions: 2//1. The website to be tested has a scrollbar3//2. Presence of a Cypress configuration file (cypress.json)45describe('General webpage functionality', () => {6 beforeEach(() => {7 cy.visit('https://www.examplewebsite.com/')8 })910 it('Verify scrollbar functionality', () => {11 //assert that the page does not initially have a scrollbar12 cy.get('body').should('not.have.property', 'scrollHeight');1314 //scroll down to the bottom of the page to check if the scrollbar appears15 cy.window().its('scrollY').should('be.eq', 0); //Assuming the page positions scrollY at 0 when at the top so that the test starts at the top.16 cy.window().scrollTo('bottom'); //Assuming the page will have enough content to allow scrolling1718 //assert that the page now has a scrollbar19 cy.get('body').should('have.property', 'scrollHeight');20 })21})2223//Connecting to remote client with desired capabilities:24describe('General webpage functionality', () => {25 before(() => {26 cy.wrap({2728 browserName: 'chrome', 29 chromeOptions: {30 args: ['--headless', '--no-sandbox'],31 },3233 }).as('chromeOptions')34 })3536 beforeEach(() => {37 cy.visit('https://www.examplewebsite.com/', {38 //set desired capabilities for the remote client39 ...Cypress.env('remote') && {40 // if remote URL is present in environment variable, connect to remote client41 ...Cypress.config('baseUrl', 'https://www.examplewebsite.com'),42 ...Cypress.config('chromeWebSecurity', false),43 ...Cypress.config('chromeOptions', {44 ...Cypress.env('chromeOptions'),45 }),46 ...Cypress.config('viewportWidth', 1024),47 ...Cypress.config('viewportHeight', 768),48 },49 })50 })5152 it('Verify scrollbar functionality', () => {53 //assert that the page does not initially have a scrollbar54 cy.get('body').should('not.have.property', 'scrollHeight');5556 //scroll down to the bottom of the page to check if the scrollbar appears57 cy.window().its('scrollY').should('be.eq', 0); //Assuming the page positions scrollY at 0 when at the top so that the test starts at the top.58 cy.window().scrollTo('bottom'); //Assuming the page will have enough content to allow scrolling5960 //assert that the page now has a scrollbar61 cy.get('body').should('have.property', 'scrollHeight');62 })63})
Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing