How to Write Playwright Locators That Survive an Upgrade

Five habits. Each one is backed by a measurement on two real releases of Swagger UI.

💡 Select Language / Dil Seçin:


🇬🇧 English

In short

A locator is the way your test finds an element on the page. When the page changes, a locator can stop working. Worse, it can keep working on the wrong element.

We tried ten ways to find one button, before and after an upgrade. Seven worked before. After the upgrade, only one was really safe. The five habits below come from that result.

What we tested

We took two real releases of Swagger UI: 4.19.1 and 5.17.14. They are the unchanged npm packages, pinned by hash. We wanted one button: the one that opens the GET /pets operation. We wrote ten locators for it.

A locator is valid if, in the old release, it finds exactly that button and nothing else. Seven were valid. Here is what they did in the new release:

Result in 5.17.14 Locators
Still right, and not by luck 1
Still right, but only because the right button comes first on the page 3
Failed with an error (Playwright refused to act on two matches) 2
Picked a different button, with no error 1

What changed in the page? In 4.19.1 the button had the name get /pets. In 5.17.14 that name moved to a new button next to it. The old button got a new name: “GET /pets List pets”. So any search by name now finds two buttons.

Habit 1: Ask for exactly one match

If your locator can match more than one element, it is not finished. Check the count. It should be 1, on the old page and on the new page.

Evidence: three locators looked fine after the upgrade. Without .first() or .nth(0) they matched 3, 6 and 3 elements. They only worked because the right element was first.

Habit 2: Treat .first() and .nth() as a warning

They turn “I found several” into “I picked one”. Playwright’s strict mode would have stopped you. These calls switch it off. Use them only when you can say why the first one is the right one, and write that reason in a comment.

Evidence: one locator ended in .first(). After the upgrade it picked the new look-alike button. No error. The test kept running.

Habit 3: Narrow the search with something stable

Look for a container that keeps the same id across versions. Then search inside it for the role of the element.

Evidence: the one safe locator was #operations-pets-get_pets .opblock-summary-control. It found exactly one button in both releases, in any order.

Habit 4: Find elements the way a user sees them, and let it fail loudly

Playwright’s own guidance is to prefer role and name, or test ids that you add on purpose. When the name moves, these locators fail with an error. That is good. A red test is better than a quiet wrong click.

Evidence: the two getByRole locators failed with a strict-mode error after the upgrade. Someone would have looked. We did not measure test ids, because this page has none. That advice is Playwright’s, not ours.

Habit 5: Check that you touched the right element

A green test does not prove you clicked the right thing. Add one check that only the right element can pass: its own name, its keyboard focus, or the state only it changes.

Evidence: the wrong button also opened the operation. A test that only checks “the operation is open” cannot tell the two apart. The wrong button has tabindex="-1", so it is not reachable by keyboard. A focus test would have caught it.

If you write XPath

Many suites use XPath, so we measured it too. Seven XPath styles aimed at the same button, on four consecutive Swagger UI releases, each release compared with the next one (three upgrades). A style is valid when, in the older release, it finds exactly that button.

XPath style 3.52.5 → 4.19.1 4.19.1 → 5.17.14 5.17.14 → 5.34.1
Absolute path (“Copy full XPath”) still right still right fails visibly
Global index (//button[...])[1] still right, only because the right button comes first same same
Id plus class still right still right still right
Content (//button[.//span[...] and ...]) still right still right still right
Per-parent index //button[...][1] not valid (matches 3) not valid not valid
contains(@aria-label, "pets") not valid (matches 2) not valid not valid
Position //div[3]//button not valid (matches 0) not valid not valid

Three rules follow from it:

  1. Do not use the path. “Copy full XPath” writes an index at every level. It survived two upgrades and failed in the third, where the structure above the button changed. It failed loudly here, not silently.
  2. Write [1] with care. //button[...][1] means “the first one inside each parent”, so it matched three buttons. (//button[...])[1] means “the first one on the page”. They are different, and the second one only worked because the right button comes first.
  3. Use an id or the content. The two styles that depended on neither a path nor an order survived all three upgrades.

The last three rows were written to include common mistakes. How often real suites make them was not measured.

Before you merge an upgrade

Count the matches of every locator on the old page and on the new page:

for (const [name, make] of Object.entries(locators)) {
  console.log(name, await make(page).count());   // should be 1 on both pages, without .first() or .nth()
}

Anything that is not 1 will fail soon, or is already hiding behind .first(). Also search your tests for .first() and .nth(.

The full result

This is the output of the script, with long lines wrapped for reading:

getByRole button, name "get /pets"          works in 4.19.1; in 5.17.14: fails visibly (strict mode: 2 matches)
getByRole button, name /get.*pets/i         works in 4.19.1; in 5.17.14: fails visibly (strict mode: 2 matches)
getByRole button, name /^GET \/pets/        not valid in 4.19.1 (matches 0)
getByText "List pets"                       not valid in 4.19.1 (matches 1, the wrong element)
CSS #operations-pets-get_pets .opblock-summary-control
                                            works in 4.19.1; in 5.17.14: still right
CSS #operations-pets-get_pets button  (.first())
                                            works in 4.19.1; in 5.17.14: still right (only because it comes first: the same selector without it matches 3)
CSS .opblock-summary-get button  (.first())
                                            works in 4.19.1; in 5.17.14: still right (only because it comes first: the same selector without it matches 6)
CSS button.opblock-summary-control  (.nth(0))
                                            works in 4.19.1; in 5.17.14: still right (only because it comes first: the same selector without it matches 3)
XPath //button[contains(@aria-label,"get")] not valid in 4.19.1 (matches 2)
CSS button[aria-label*="pets"]  (.first())  works in 4.19.1; in 5.17.14: SILENTLY WRONG (opblock-control-arrow; the same selector without .first() matches 2); clicking it still opens the operation

10 locator styles tried, 7 valid in 4.19.1. In 5.17.14: 4 still right (3 of them only because the right element comes first in the DOM), 2 fail visibly, 1 silently wrong.

The script prints each of these on one line. Two runs in a row printed the same text.

What we did not measure

Run it yourself

cd examples/js-playwright-capture
npm install
node locator-survival.mjs ../../TestAutomation/ScenarioRunner/Fixtures/organic-drift/swagger-ui/manifest.json --channel chrome
node xpath-survival.mjs ../../TestAutomation/ScenarioRunner/Fixtures/organic-drift/swagger-ui/xpath-manifest.json --channel chrome

A release whose hash does not match the manifest is refused. --channel chrome uses an installed Chrome. Without it, run npx playwright install chromium first.


🇹🇷 Türkçe

Kısaca

Locator, testin sayfada bir elemanı bulma yöntemidir. Sayfa değişince bir locator çalışmayı bırakabilir. Daha kötüsü, yanlış elemanda çalışmaya devam edebilir.

Bir butonu bulmak için on yöntemi, yükseltmeden önce ve sonra denedik. Yedisi önce çalıştı. Yükseltmeden sonra yalnızca biri gerçekten sağlamdı. Aşağıdaki beş alışkanlık bu sonuçtan çıkıyor.

Ne denedik

Swagger UI’nin iki gerçek sürümünü aldık: 4.19.1 ve 5.17.14. Değiştirilmemiş npm paketleri, hash ile sabitlenmiş. GET /pets operasyonunu açan butonu istedik ve bunun için on locator yazdık.

Eski sürümde tam o butonu bulan locator “geçerli” sayıldı. Yedisi geçerliydi. Yeni sürümde şunları yaptılar:

5.17.14’teki sonuç Locator
Hâlâ doğru, şansa bağlı değil 1
Hâlâ doğru, ama yalnızca doğru buton sayfada önce geldiği için 3
Hata verdi (Playwright iki eşleşmede işlem yapmayı reddetti) 2
Hata vermeden başka bir buton seçti 1

Sayfada ne değişti? 4.19.1’de butonun adı get /pets idi. 5.17.14’te bu ad, yanındaki yeni bir butona taşındı. Eski butonun adı “GET /pets List pets” oldu. Yani isimle arayan herkes artık iki buton buluyor.

Alışkanlık 1: Tam bir eşleşme isteyin

Locator’ınız birden fazla elemana uyabiliyorsa bitmemiştir. Sayıyı kontrol edin. Eski sayfada da yeni sayfada da 1 olmalı.

Kanıt: üç locator yükseltmeden sonra iyi görünüyordu. .first() ya da .nth(0) olmadan 3, 6 ve 3 elemanla eşleşiyorlardı. Yalnızca doğru eleman ilk sırada olduğu için çalıştılar.

Alışkanlık 2: .first() ve .nth() bir uyarıdır

“Birkaç tane buldum” cümlesini “birini seçtim” yapar. Playwright’ın strict modu sizi durdururdu. Bu çağrılar onu kapatır. Yalnızca ilkinin neden doğru olduğunu söyleyebiliyorsanız kullanın ve nedenini yoruma yazın.

Kanıt: bir locator .first() ile bitiyordu. Yükseltmeden sonra yeni benzer butonu seçti. Hata yok. Test çalışmaya devam etti.

Alışkanlık 3: Aramayı sabit bir şeyle daraltın

Sürümler arasında aynı kalan bir kapsayıcı id’si bulun. Sonra elemanın rolünü onun içinde arayın.

Kanıt: tek sağlam locator #operations-pets-get_pets .opblock-summary-control idi. İki sürümde de, sıradan bağımsız, tam bir buton buldu.

Alışkanlık 4: Elemanı kullanıcının gördüğü gibi bulun ve gürültüyle başarısız olmasına izin verin

Playwright’ın kendi tavsiyesi rol ve ad, ya da bilerek eklediğiniz test id’leridir. Ad taşınınca bu locator’lar hata verir. Bu iyidir. Kırmızı bir test, sessiz bir yanlış tıklamadan iyidir.

Kanıt: iki getByRole locator yükseltmeden sonra strict-mode hatası verdi. Biri bakardı. Test id’lerini ölçmedik, çünkü bu sayfada yok. Bu tavsiye Playwright’ın, bizim değil.

Alışkanlık 5: Doğru elemana dokunduğunuzu kontrol edin

Yeşil test, doğru şeye tıkladığınızı kanıtlamaz. Yalnızca doğru elemanın geçebileceği bir kontrol ekleyin: kendi adı, klavye odağı ya da yalnızca onun değiştirdiği durum.

Kanıt: yanlış buton da operasyonu açtı. Yalnızca “operasyon açık” diye bakan bir test ikisini ayıramaz. Yanlış butonda tabindex="-1" var, klavyeyle ulaşılamıyor. Bir odak testi bunu yakalardı.

XPath yazıyorsanız

Birçok pakette XPath kullanılıyor, o yüzden onu da ölçtük. Aynı butona yönelen yedi XPath stili, art arda dört Swagger UI sürümünde, her sürüm bir sonrakiyle karşılaştırıldı (üç yükseltme). Eski sürümde tam o butonu bulan stil “geçerli” sayıldı.

XPath stili 3.52.5 → 4.19.1 4.19.1 → 5.17.14 5.17.14 → 5.34.1
Mutlak yol (“Copy full XPath”) hâlâ doğru hâlâ doğru açık hata verdi
Genel sıra (//button[...])[1] hâlâ doğru, yalnızca doğru buton önce geldiği için aynı aynı
Kimlik ve sınıf hâlâ doğru hâlâ doğru hâlâ doğru
İçerik (//button[.//span[...] and ...]) hâlâ doğru hâlâ doğru hâlâ doğru
Ebeveyn başına sıra //button[...][1] geçerli değil (3 eşleşme) geçerli değil geçerli değil
contains(@aria-label, "pets") geçerli değil (2 eşleşme) geçerli değil geçerli değil
Konum //div[3]//button geçerli değil (0 eşleşme) geçerli değil geçerli değil

Bundan üç kural çıkıyor:

  1. Yolu kullanmayın. “Copy full XPath” her seviyede bir sıra numarası yazar. İki yükseltmede ayakta kaldı, üçüncüde düştü: butonun üstündeki yapı değişmişti. Burada sessizce değil, açık hatayla düştü.
  2. [1] yazarken dikkat edin. //button[...][1] “her ebeveynin içindeki ilk” demek, bu yüzden üç butonla eşleşti. (//button[...])[1] ise “sayfadaki ilk” demek. İkisi farklı ve ikincisi yalnızca doğru buton önce geldiği için çalıştı.
  3. Kimlik ya da içerik kullanın. Ne yola ne de sıraya bağlı olan iki stil, üç yükseltmenin üçünde de ayakta kaldı.

Son üç satır yaygın hataları içerecek şekilde yazıldı. Gerçek paketlerde ne sıklıkla yapıldıklarını ölçmedik.

Bir yükseltmeyi birleştirmeden önce

Her locator’ın eşleşme sayısını eski sayfada ve yeni sayfada sayın (yukarıdaki kod). 1 olmayan her şey yakında hata verecek ya da zaten .first() arkasına saklanıyor. Testlerinizde .first() ve .nth( de arayın.

Ölçmediklerimiz

Tek program (Swagger UI). Locator ölçümü tek yükseltme (4.19.1’den 5.17.14’e), XPath ölçümü üç yükseltme (3.52.5, 4.19.1, 5.17.14, 5.34.1). On locator’ı biz seçtik, gerçek test paketlerinin örneği değil, hangisinin ne kadar yaygın olduğunu bilmiyoruz. “Geçerli” tanımı bizim. Tıklama etkisi yalnızca bu sayfada denendi. Yeniden üretmek için İngilizce bölümdeki komutu çalıştırın.


See also