Skip to content

Commit ad545fd

Browse files
committed
Add new til
1 parent 81e2672 commit ad545fd

3 files changed

+71
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Use Object.hasOwn to check Object properties in JavaScript
2+
3+
The `Object.hasOwn()` is a modern JavaScript feature that allows you to check if a given property exists in an object. It's recommended over the `Object.prototype.hasOwnProperty()` function since [it's safer and more intuitive](<https://igorgonchar.medium.com/javascript-hasown-new-way-to-check-if-object-has-property-b93810e47070#:~:text=The%20main%20difference%20is%20that,checks%20only%20the%20object%20itself.&text=hasOwnProperty()%20besides%20focusing%20only,also%20omits%20getters%20and%20setters.>)
4+
5+
For example, if we have a custom object of a person:
6+
7+
```js
8+
const person = {
9+
name: 'John',
10+
age: 24,
11+
gender: 'male',
12+
bloodType: 'O',
13+
};
14+
```
15+
16+
We can use `Object.hasOwn()` function to check if the `bloodType` property is present in the `person` object:
17+
18+
```js
19+
Object.hasOwn(person, 'bloodType'); // true
20+
```
21+
22+
The above example would have the same result as calling `Object.proptype.hasOwnerProperty.call(person, 'bloodType');`. However, `Object.hasOwn()` is much shorter and more convenient to use.
23+
24+
Here is another example of checking the property in a loop:
25+
26+
```js
27+
const people = [
28+
{
29+
name: 'Carly',
30+
yearOfBirth: 1942,
31+
yearOfDeath: 1970,
32+
},
33+
{
34+
name: 'Ray',
35+
yearOfBirth: 1962,
36+
yearOfDeath: 2011,
37+
},
38+
{
39+
name: 'Jane',
40+
yearOfBirth: 1912,
41+
yearOfDeath: 1941,
42+
},
43+
{
44+
name: 'Josh',
45+
yearOfBirth: 1933,
46+
},
47+
];
48+
49+
const findTheOldest = (array) =>
50+
array.filter((person) => Object.hasOwn(person, 'yearOfDeath'));
51+
52+
console.log(findTheOldest(people));
53+
```
54+
55+
References:
56+
57+
- [Object.hasOwn()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn) - MDN docs
58+
- [JavaScript: hasOwn() — new way to check if Object has property](<https://igorgonchar.medium.com/javascript-hasown-new-way-to-check-if-object-has-property-b93810e47070#:~:text=The%20main%20difference%20is%20that,checks%20only%20the%20object%20itself.&text=hasOwnProperty()%20besides%20focusing%20only,also%20omits%20getters%20and%20setters.>)

python/use-playwright-codegen-to-automatically-record-ui-interactions.md renamed to python/use-playwright-codegen-to-automate-ui-interactions.md

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
1-
# Use Playwright Codegen to Automatically Record UI Interactions
1+
# Use Playwright Codegen to Automate UI Interactions
22

3-
Playwright has a neat feature of recording user interaction and automatically
4-
generate source code for you. This would save you a lot of time and automate many
5-
of your day to day work if the tasks can be done by a browser.
3+
Playwright has a useful codegen feature that allows you to record your user interactions and generate source code automatically, saving you time and effort in your day-to-day work. If your tasks can be completed through a browser, Codegen can automate them for you.
64

7-
The following example will show you how to record an UI operation that we visit
8-
the playwright.dev docs and search for this codegen feature and then move on to
9-
the next page:
5+
In the following example, we'll demonstrate how to record an UI interactions that involves visiting the playwright.dev documentation, searching for the Codegen feature docs and navigating to the next page.
106

11-
First, create a venv:
7+
To get started, create a virtual environment:
128

139
```sh
1410
python3 -m venv venv
1511
```
1612

17-
Then, install the package
13+
Next, install the `pytest-playwright` package:
1814

1915
```sh
2016
pip install pytest-playwright
2117
```
2218

23-
And run the codegen command:
19+
Then, run the codegen command:
2420

2521
```sh
2622
playwright codegen
2723
```
2824

29-
> Note that you might need to run `playwright install` command if you haven't installed any chromium browser on your machine before
25+
Note that if you haven't installed any Chromium browser on your machine before, you may need to run the `playwright install` command.
3026

31-
After running the codegen, you should now have two browsers opened, one is the main chromium browser and the other is a UI for recorded actions.
27+
Once you've run the codegen, two browser windows should open - one main Chromium browser and an UI for recording your UI interactions.
3228

33-
Here is a piece of code that's generated by the codegen after running thru the actions that we mentioned in the above:
29+
Here is an example of the code that is generated by the codegen after performing the actions mentioned above:
3430

3531
```py
3632
import time
@@ -54,10 +50,10 @@ def run(playwright: Playwright) -> None:
5450
page.get_by_role(
5551
"heading", name="Running CodegenDirect link to Running Codegen").click()
5652

57-
# The next page link is located in the bottom of the view
53+
# The next page link is located at the bottom of the view
5854
page.mouse.wheel(0, 4000)
5955

60-
# These sleeps help to slow down the UI interactions so you can see what's actually done during the run
56+
# These sleeps commands help to slow down the UI interactions, allowing you to observe what is happening during the run.
6157
time.sleep(2)
6258
page.locator(".pagination-nav__link--next").click()
6359

readme.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Inspired by [Josh Branchaud](https://dev.to/jbranchaud/how-i-built-a-learning-ma
88

99
- [Get All Elements From a Form](js/get-all-elements-from-a-form.md)
1010
- [Convert NodeList and HTMLCollection to JS Array](js/convert-nodelist-and-htmlcollection-to-js-array.md)
11+
- [Use Object.hasOwn to check Object properties in JavaScript](js/use-object-hasown-to-check-properties-in-javascript.md)
1112

1213
## Python
1314

@@ -16,7 +17,7 @@ Inspired by [Josh Branchaud](https://dev.to/jbranchaud/how-i-built-a-learning-ma
1617
- [Display Web Scraping Data with Pandas](/python/display-web-scraping-data-with-pandas.md)
1718
- [Scrape Data From an API Endpoint](/python/scrape-data-from-an-api-endpoint.md)
1819
- [Scrape Dynamic Data with Playwright](/python/scrape-dynamic-data-with-playwright.md)
19-
- [Use Playwright Codegen to Automatically Record UI Interactions](python/use-playwright-codegen-to-automatically-record-ui-interactions.md)
20+
- [Use Playwright Codegen to Automate UI Interactions](python/use-playwright-codegen-to-automate-ui-interactions.md)
2021

2122
## CSS
2223

0 commit comments

Comments
 (0)