Repository: nightwatchjs/nightwatch
Pull request #4121 (2024-03-12 19:15:40)

fixed `takeScreenshot` example
Fixes: #4120 
Thanks in advance for your contribution. Please follow the below steps in submitting a pull request, as it will help us with reviewing it quicker.

- [x] Create a new branch from master (e.g. `features/my-new-feature` or `issue/123-my-bugfix`);
- [x] If you're fixing a bug also create an issue if one doesn't exist yet;
- [x] If it's a new feature explain why do you think it's necessary. Please check with the maintainers beforehand to make sure it is something that we will accept. Usually we only accept new features if we feel that they will benefit the entire community;
- [x] Please avoid sending PRs which contain drastic or low level changes. If you are certain that the changes are needed, please discuss them beforehand and indicate what the impact will be;
- [x] If your change is based on existing functionality please consider refactoring first. Pull requests that duplicate code will most likely be ignored;
- [x] Do not include changes that are not related to the issue at hand;
- [x] Follow the same coding style with regards to spaces, semicolons, variable naming etc.;
- [ ] Always add unit tests - PRs without tests are most of the times ignored.


github-actions[bot]: 
## Status
* ❌ No modified files found in the **types** directory.
Please make sure to include types for any changes you have made. Thank you!.

Patch:
diff --git a/lib/api/web-element/commands/takeScreenshot.js b/lib/api/web-element/commands/takeScreenshot.js
index 9f4ab1b2..0818ebd0 100644
--- a/lib/api/web-element/commands/takeScreenshot.js
+++ b/lib/api/web-element/commands/takeScreenshot.js
@@ -6,13 +6,13 @@
  *   demoTest(browser: NightwatchAPI): void {
  *     const screenshot = browser.element('#main').takeScreenshot();
  *     screenshot.then((screenshotData) => {
- *       require('fs').writeFile('out.png', screenshotData, 'base64');
+ *       require('fs/promises').writeFile('out.png', screenshotData, 'base64');
  *     });
  *   },
  *
  *   async demoTestAsync(browser: NightwatchAPI): Promise<void> {
  *     const screenshotData = await browser.element('#main').takeScreenshot();
- *     require('fs').writeFile('out.png', screenshotData, 'base64');
+ *     require('fs/promises').writeFile('out.png', screenshotData, 'base64');
  *   }
  * }
  *
diff --git a/lib/api/web-element/scoped-element.js b/lib/api/web-element/scoped-element.js
index 69cd4212..32b1e4f4 100644
--- a/lib/api/web-element/scoped-element.js
+++ b/lib/api/web-element/scoped-element.js
@@ -24,7 +24,8 @@ class ScopedWebElement {
       'findAllByAltText': ['getAllByAltText'],
       'getRect': ['getSize', 'getLocation'],
       'getProperty': ['property'],
-      'getCssProperty': ['css']
+      'getCssProperty': ['css'],
+      'isVisible': ['isDisplayed']
     };
   }
 
diff --git a/types/web-element.d.ts b/types/web-element.d.ts
index dc2f87aa..1547fc7e 100644
--- a/types/web-element.d.ts
+++ b/types/web-element.d.ts
@@ -194,6 +194,7 @@ export interface ScopedElement extends Element, PromiseLike<WebElement> {
   isEnabled(): ElementValue<boolean>;
 
   isVisible(): ElementValue<boolean>;
+  isDisplayed(): ElementValue<boolean>;
 }
 
 type WaitUntilOptions = {


Test patch:
diff --git a/test/src/api/commands/web-element/testIsVisible.js b/test/src/api/commands/web-element/testIsVisible.js
index c1ba6c97..ed2fddf5 100644
--- a/test/src/api/commands/web-element/testIsVisible.js
+++ b/test/src/api/commands/web-element/testIsVisible.js
@@ -41,6 +41,33 @@ describe('element().isVisible() command', function() {
     assert.strictEqual(elementId, '0');
   });
 
+  it('test .element().isDisplayed() displayed', async function() {
+    let elementId;
+
+    MockServer.addMock({
+      url: '/session/13521-10219-202/execute/sync',
+      method: 'POST',
+      response: JSON.stringify({
+        value: true
+      }),
+      onRequest(_, requestData) {
+        elementId = requestData.args[0]['element-6066-11e4-a52e-4f735466cecf'];
+      }
+    }, true);
+
+    const resultPromise = this.client.api.element('#signupSection').isDisplayed();
+    assert.strictEqual(resultPromise instanceof Element, false);
+    assert.strictEqual(typeof resultPromise.find, 'undefined');
+
+    assert.strictEqual(resultPromise instanceof Promise, false);
+    assert.strictEqual(typeof resultPromise.then, 'function');
+
+    const result = await resultPromise;
+    assert.strictEqual(result instanceof WebElement, false);
+    assert.strictEqual(result, true);
+    assert.strictEqual(elementId, '0');
+  });
+
   it('test .element().isVisible() not displayed', async function() {
     let elementId;
 
@@ -68,6 +95,33 @@ describe('element().isVisible() command', function() {
     assert.strictEqual(elementId, '0');
   });
 
+  it('test .element().isDisplayed() not displayed', async function() {
+    let elementId;
+
+    MockServer.addMock({
+      url: '/session/13521-10219-202/execute/sync',
+      method: 'POST',
+      response: JSON.stringify({
+        value: false
+      }),
+      onRequest(_, requestData) {
+        elementId = requestData.args[0]['element-6066-11e4-a52e-4f735466cecf'];
+      }
+    }, true);
+
+    const resultPromise = this.client.api.element('#signupSection').isDisplayed();
+    assert.strictEqual(resultPromise instanceof Element, false);
+    assert.strictEqual(typeof resultPromise.find, 'undefined');
+
+    assert.strictEqual(resultPromise instanceof Promise, false);
+    assert.strictEqual(typeof resultPromise.then, 'function');
+
+    const result = await resultPromise;
+    assert.strictEqual(result instanceof WebElement, false);
+    assert.strictEqual(result, false);
+    assert.strictEqual(elementId, '0');
+  });
+
   it('test .element().find().isVisible()', async function() {
     let elementId;
 
diff --git a/types/tests/webElement.test-d.ts b/types/tests/webElement.test-d.ts
index 78edaa90..8a3172f9 100644
--- a/types/tests/webElement.test-d.ts
+++ b/types/tests/webElement.test-d.ts
@@ -142,6 +142,7 @@ describe('new element() api', function () {
     expectType<ElementValue<string | null>>(elem.getValue());
     expectType<ElementValue<boolean>>(elem.isEnabled());
     expectType<ElementValue<boolean>>(elem.isVisible());
+    expectType<ElementValue<boolean>>(elem.isDisplayed());
 
     expectType<ElementValue<ScopedElementRect>>(elem.getRect());
     expectType<ElementValue<ScopedElementRect>>(elem.getSize());